开发者

Count Distinct with NULL retention

开发者 https://www.devze.com 2023-01-19 05:37 出处:网络
I am using SQL 2005 and having a simple query as below trap duplicates: SELECT x,y,COUNT(DISTINCT z) AS z_count

I am using SQL 2005 and having a simple query as below trap duplicates:

SELECT x,y,COUNT(DISTINCT z) AS z_count 
FROM tblA 
GROUP BY x,y 
HAVING 开发者_如何学Python(COUNT(DISTINCT z) > 1)

Now the issue is this column z is sometimes having NULL values, which get ignored by count distinct. As such, duplicates having z as NULL in one record and as not-NULL in another, are not getting trapped.

Can anyone plz suggest how can I get around with this with one single query?


Quick and dirty solution: Replace null by dummy-null value:

SELECT x,y,COUNT(DISTINCT Coalesce(z, 'dummy-null')) AS z_count 
FROM tblA 
GROUP BY x,y 
HAVING (COUNT(DISTINCT Coalesce(z, 'dummy-null')) > 1)

Only possible, if you can fake a dummy-null value.

Edit: I guess you use Count(distinct z) because rows having x=y=z shoud not be considered duplicates (for some reason). Otherwise, to get ALL duplicates of x=y use:

SELECT x,y,COUNT(*) AS dup_count
FROM tblA 
GROUP BY x,y
HAVING (COUNT(*) > 1)
0

精彩评论

暂无评论...
验证码 换一张
取 消