我正在SSMS 2005中设计一个查询,如下所示:
SELECT COUNT(DISTINCT ColumnName) FROM Table WHERE ColumnName IS NOT NULL
当我使用COUNT()运行查询时,它返回值1.当我在没有COUNT()的情况下运行它时,SSMS会报告正确的值,例如212条记录。
有问题的列是数据类型numeric(16,0)。
对于那些可能会问的人,查询完整是:
SELECT COUNT(DISTINCT O_ID) FROM vEmployers
INNER JOIN vEnrolment ON O_ID = E_EnrolmentEmployer
WHERE E_START >= '01-AUG-2008' AND E_START < '01-AUG-2009'
AND O_ID IS NOT NULL AND O_ID IN (
SELECT O_ID FROM vEmployers
INNER JOIN vEnrolment ON O_ID = E_EnrolmentEmployer
WHERE E_Start < '01-AUG-2008' and E_Start >= '01-AUG-2007'
)
此查询基本上给出了两个12个月之间的重复业务数据。
所以我想知道为什么“COUNT(DISTINCT ColumnName)”在指定“ColumnName IS NOT NULL”时返回1?
以下是运行SELECT TOP 10 DISTINCT ColumnName FROM ...等时的数据示例:
1346116
1346131
1346425
1346923
1349935
1350115
1350153
2594787
2821944
2879631
答案 0 :(得分:5)
使用数字(16,0)让我怀疑它与数据类型有关。在COUNT子句中添加CAST以将其强制转换为INT类型:
Count(Distinct Cast(O_ID as Int))
答案 1 :(得分:0)
我猜是因为返回的所有行共享O_ID
的相同值。您可以对每行唯一的密钥执行COUNT(*)
或COUNT()
以获取行数。
答案 2 :(得分:0)
删除DISTINCT,您将获得所有行的计数。
答案 3 :(得分:0)
请你运行这些查询:
SELECT COUNT(DISTINCT O_ID)
FROM vEmployers
INNER JOIN
vEnrolment
ON O_ID = E_EnrolmentEmployer
WHERE E_START >= '01-AUG-2008' AND
E_START < '01-AUG-2009'
AND O_ID IN
(
SELECT O_ID
FROM vEmployers
INNER JOIN
vEnrolment
ON O_ID = E_EnrolmentEmployer
WHERE E_Start < '01-AUG-2008'
AND E_Start >= '01-AUG-2007'
)
和
SELECT DISTINCT TOP 5 O_ID
FROM vEmployers
INNER JOIN
vEnrolment
ON O_ID = E_EnrolmentEmployer
WHERE E_START >= '01-AUG-2008' AND
E_START < '01-AUG-2009'
AND O_ID IN
(
SELECT O_ID
FROM vEmployers
INNER JOIN
vEnrolment
ON O_ID = E_EnrolmentEmployer
WHERE E_Start < '01-AUG-2008'
AND E_Start >= '01-AUG-2007'
)
ORDER BY
O_ID
逐字,没有改变什么?
答案 4 :(得分:0)
SELECT
COUNT(*)
FROM vEmployers
INNER JOIN vEnrolment ON O_ID = E_EnrolmentEmployer
WHERE
E_START >= '01-AUG-2008'
AND E_START < '01-AUG-2009'
AND O_ID IS NOT NULL AND O_ID IN (
SELECT O_ID FROM vEmployers
INNER JOIN vEnrolment ON O_ID = E_EnrolmentEmployer
WHERE E_Start < '01-AUG-2008' and E_Start >= '01-AUG-2007'
)
GROUP BY
O_Id