SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SELECT COUNT
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

divan
Posting Yak Master

153 Posts

Posted - 04/10/2013 :  07:58:56  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 04/10/2013 :  08:15:02  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 04/11/2013 :  02:16:39  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000