所以这是我所面对的问题 - 我有一张桌子" A"如下
Name AmountPaid Type
NAME1 5 1
NAME1 10 1
NAME1 3 1
NAME2 5 2
NAME2 9 2
NAME2 8 2
这里我们有两个名字 - 名字1和名字2,类型1和2
我正在尝试获取一个查询,该查询应仅返回数字,该数字应显示表中存在多少次唯一名称集合。 在上面我希望结果为 - 2 我试过了查询
Select NAME, count(Name) from A group by Name
然而,这将返回2个以下的记录。
NAME1 3
NAME2 3
非常感谢任何帮助。
答案 0 :(得分:3)
请尝试以下查询:
Select count(distinct(NAME)) from A
答案 1 :(得分:2)
如果没有错,这就是你需要的。考虑到count
如果不重复,则name
SELECT Count(DISTINCT NAME)
FROM (SELECT *,
Row_number()OVER(partition BY NAME ORDER BY type) rn
FROM Yourtable) a
WHERE rn > 1
答案 2 :(得分:1)
您可以使用DISTINCT执行此操作:
SELECT COUNT (DISTINCT NAME)
FROM A
答案 3 :(得分:0)
使用它:
--TEST TABLE
Declare @sometable as table (Name Varchar(10))
insert into @sometable
values ('Name1'),
('Name1'),
('Name1'),
('Name2'),
('Name2'),
('Name2'),
('Name3'),
('Name4')
--VARIANT 1
SELECT COUNT(*)
FROM (
SELECT NAME,
COUNT(*) AS Cnt
FROM @sometable
GROUP BY NAME
) AS T
WHERE Cnt > 1
--VARIANT 2
SELECT COUNT(*)
FROM ( SELECT NAME
FROM @sometable
GROUP BY NAME
HAVING COUNT(*) > 1 ) AS T
--VARIANT 3
SELECT COUNT(*)
FROM (SELECT DISTINCT NAME, COUNT(*) OVER (PARTITION BY NAME) AS Cnt
FROM @sometable
) AS T
WHERE Cnt > 1