Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CountDistinct in SQL Server
Message
From
01/04/2008 11:43:45
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01307343
Message ID:
01307374
Views:
10
>>>>There is a CountDistinct function in Reporting Services that "Returns the number of non-null unique instances of an item within a set". I would like to do that in a SQL query, but can't seem to find my brain this morning. There are just three items in the query, ItemA, ItemB, and the Number (or count) of unique values for ItemB with respect to itself. In other words, if ItemB had values P1, P3, and P7, and if P1 occurred 347 times, and P3 occurred 231, etc., these values would appear in ColumnC.
>>>>
>>>>All help is appreciated.
>>>
>>>
>>>SELECT ItemA, COUNT(DISTINCT ItemB ) FROM mytable GROUP BY ItemA
>>>
>>
>>Well, here is the exact code, and it includes ItemB (program), which seems to through things off.
>>
>>
>>SELECT     PersonID, Program, COUNT(DISTINCT Program) AS ProgramCount
>>FROM         dbo.tmmLicesnses2Persons
>>WHERE     (DateActivated IS NOT NULL) AND (DateDeactivated IS NULL)
>>GROUP BY PersonID, Program
>>
>
>Not sure what you want, but I suppose you wan to count DISTINCT program for each PersonId:
>
>SELECT     PersonID, Program, Tbl1.ProgramCount
>FROM         dbo.tmmLicesnses2Persons
>INNER JOIN (SELECT PersonID, COUNT(DISTINCT Program) AS ProgramCount
>                   FROM dbo.tmmLicesnses2Persons
>            GROUP BY PersonID) Tbl1
>      ON tmmLicesnses2Persons.PersonID = Tbl1.PersonID
>WHERE     (DateActivated IS NOT NULL) AND (DateDeactivated IS NULL)
>
I appreciate your help Borislav, thank you!
Thank You

Rollin Burr

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

Click here to load this message in the networking platform