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
 Coalesce/NULL (Count) Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Chloe_19
Starting Member

44 Posts

Posted - 03/21/2012 :  23:15:37  Show Profile  Reply with Quote



This code below gives me the following table:

ID       PROVIDER_CDE     GRF_COUNT   ACU_COUNT   
100034     GRF               4              1          
100054     GRF               3              1



SELECT Distinct
a.id, a.PROVIDER_CDE,  a.GRF_COUNT, E.ACU_COUNT       

FROM
(SELECT *
FROM
(SELECT id, PROVIDER_CDE, count(*) GRF_COUNT
FROM enrolment
WHERE Provider_cde = 'GRF'


GROUP BY id, PROVIDER_CDE))A

INNER JOIN
(SELECT *
FROM(
SELECT ID,PROVIDER_CDE, count(*) ACU_COUNT
FROM enrolment 
WHERE Provider_cde = 'ACU'
GROUP ID, PROVIDER_CDE))E
ON ID = ID
ORDER BY ID ASC



But it only gives me the ID where there is a count.
I need all the ID even if there is no count for example if i wanted to add another Column CLM_COUNT (below) all ID should be shown, not only the once where these is a count.

ID     PROVIDER_CDE   GRF COUNT   ACU_COUNT    CLM_COUNT
100034  GRF               4              1            0
100054  GRF               3              1            0


khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 03/21/2012 :  23:23:33  Show Profile  Reply with Quote
do you have another table that store all the ID and PROVIDER_CDE ?


KH
Time is always against us

Go to Top of Page

Chloe_19
Starting Member

44 Posts

Posted - 03/21/2012 :  23:28:33  Show Profile  Reply with Quote
All data is taken from the same table (Enrolment).
No, I am only quering it. Not putting it into other tables.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 03/21/2012 :  23:40:49  Show Profile  Reply with Quote

SELECT 	ID, PROVIDER_CDE,
	COUNT(CASE WHEN GRF_COUNT = 'GRF' THEN 1 END) as GRF_COUNT,
	COUNT(CASE WHEN GRF_COUNT = 'ACU' THEN 1 END) as ACU_COUNT,
        COUNT(*) as CLM_COUNT
FROM	enrolment
WHERE	PROVIDER_CDE	IN ('GRF', 'ACU')
GROUP BY ID, PROVIDER_CDE



KH
Time is always against us

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