Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
CountDistinct in SQL Server
Message
 
 
À
01/04/2008 11:31:24
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01307343
Message ID:
01307376
Vues:
9
Using derived tables
	SELECT dt1.PersonID, dt1.Program, dt2.ProgramCount
		FROM (SELECT DISTINCT PersonID, Program FROM tmmLicesnses2Persons
			WHERE (DateActivated IS NOT NULL) AND (DateDeactivated IS NULL)) dt1
		JOIN (SELECT COUNT(*) AS ProgramCount FROM dbo.tmmLicesnses2Persons  
				WHERE (DateActivated IS NOT NULL) AND (DateDeactivated IS NULL)
				GROUP BY Program) dt2 
			ON dt1.Program = dt2.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
--sb--
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform