Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
CountDistinct in SQL Server
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01307343
Message ID:
01307367
Vues:
6
This message has been marked as a message which has helped to the initial question of the thread.
>>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)
>
Great minds... But I think we need to repeat where condition twice (in both selects).
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform