sql分组查询的问题
假如有表格a
列数有,查找出相同名称的基础上,UOM存在不同的记录,如下示例数据
name UOM
A 1
A 1
B 2
B 2
B 3
C 1
C 1
D 2
D 3
D 4
查找的结果是
B
D
如果能显示不同的UOM的数量更好如
B 2
D 3
------解决方案--------------------select name,count(UOM) as num from
(select distinct * from a) a group by name having count(UOM)> 1
------解决方案--------------------Create Table A
(name Varchar(10),
UOM Int)
Insert A Select 'A ', 1
Union All Select 'A ', 1
Union All Select 'B ', 2
Union All Select 'B ', 2
Union All Select 'B ', 3
Union All Select 'C ', 1
Union All Select 'C ', 1
Union All Select 'D ', 2
Union All Select 'D ', 3
Union All Select 'D ', 4
GO
Select name, Count(Distinct UOM) As Count From A Group By name Having Count(Distinct UOM) > 1
GO
Drop Table A
--Result
/*
name Count
B 2
D 3
*/