Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CountDistinct in SQL Server
Message
From
01/04/2008 11:41:43
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01307343
Message ID:
01307370
Views:
9
>
>SELECT  tlp.PersonID, tlp.Program,
>		( SELECT COUNT(*) FROM dbo.tmmLicesnses2Persons tlp2
>			WHERE tlp2.Program = tlp.Program
>				AND (DateActivated IS NOT NULL) AND (DateDeactivated IS NULL)) AS ProgramCount
>	FROM   dbo.tmmLicesnses2Persons tlp
>	WHERE (DateActivated IS NOT NULL) AND (DateDeactivated IS NULL)
>	GROUP BY PersonID, Program
>
>>>
>>>SELECT     PersonID, Program, COUNT(*) AS ProgramCount
>>>FROM         dbo.tmmLicesnses2Persons
>>>WHERE     (DateActivated IS NOT NULL) AND (DateDeactivated IS NULL)
>>>GROUP BY PersonID, Program
>>>
>>
>>This returns:
>>
>>1 P1 1
>>1 P2 1
>>2 P1 1
>>2 P2 1
>>3 P2 1
>>3 P3 1
>>3 P4 1
>>3 P5 1
>>
>>What I need is:
>>
>>1 P1 2
>>1 P2 3
>>2 P1 2
>>2 P2 3
>>3 P2 3
>>3 P3 1
>>3 P4 1
>>3 P5 1

This is the solution I was looking for, simple and correct. I have actually done this before, but could not wrap my mind around it this morning. Thank you Sergey!
Thank You

Rollin Burr

Politicians and diapers have one thing in common. They should both be changed regularly, and for the same reason.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform