Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SELECT COUNT

Author  Topic 

divan
Posting Yak Master

153 Posts

Posted - 2013-04-10 : 07:58:56
I have a table that has a column named RELATIONSHIP_CD where the code can be H,G, T and also NULL..

I have written a script

SELECT COUNT(C.RELATIONSHIP_CD) ,C.RELATIONSHIP, E.DESCRIPTION
FROM CLAIM C
LEFT INNER JOIN EDIT_LONG_CODE E ON E.TBNAME = 'CLAIM' AND E.NAME = 'RELATIONSHIP' AND E.CODE = C.RELATIONSHIP
GROUP BY C.RELATIONSHIP, E.DESCRIPTION

when I run the script I get the correct count for all the codes but for the Null I get 0 (zero) even though there are 34 records that have null so what am I missing or doing wrong..

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-10 : 08:15:02
SELECT COUNT(*) ,C.RELATIONSHIP, E.DESCRIPTION
FROM CLAIM C
LEFT INNER JOIN EDIT_LONG_CODE E ON E.TBNAME = 'CLAIM' AND E.NAME = 'RELATIONSHIP' AND E.CODE = C.RELATIONSHIP
GROUP BY C.RELATIONSHIP, E.DESCRIPTION

COUNT(*) --> will count NULLs
COUNT(ColumnName) --> will never consider NULLs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-11 : 02:16:39
if attempt is to count instances of RELATIONSHIP_CD you may be better off putting it as

SELECT COUNT(*) AS Cnt,C.RELATIONSHIP_CD
FROM CLAIM C
LEFT JOIN EDIT_LONG_CODE E
ON E.TBNAME = 'CLAIM'
AND E.NAME = 'RELATIONSHIP'
AND E.CODE = C.RELATIONSHIP
GROUP BY C.RELATIONSHIP, E.DESCRIPTION,C.RELATIONSHIP_CD

so that you can see code values side by side with counts

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -