Hi,I have a aggregate query that calculates pass/ fail percentages. I want to be able to run it based on different groupings. Is there any easy way to do this other than writing a separate query for each Grouping level required? In the example below, I want to be able to choose between Grouping on Level1 or Level2[code="plain"]CREATE TABLE dbo.Table_1 ( ID int NOT NULL IDENTITY (1, 1), Level1 varchar(50) NOT NULL, Level2 varchar(50) NOT NULL, Result varchar(10) NOT NULL ) ON [PRIMARY]GOALTER TABLE dbo.Table_1 ADD CONSTRAINT PK_Table_1 PRIMARY KEY CLUSTERED ( ID ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY][/code]The query would then be:[code="plain"]SELECT Level1 , COUNT(CASE WHEN Result = 'PASS' THEN 1 ELSE 0 END) AS Passes , COUNT(CASE WHEN Result = 'FAIL' THEN 1 ELSE 0 END) AS FailsFROM Table_1GROUP BY Level1[/code]or [code="plain"]SELECT Level2 , COUNT(CASE WHEN Result = 'PASS' THEN 1 ELSE 0 END) AS Passes , COUNT(CASE WHEN Result = 'FAIL' THEN 1 ELSE 0 END) AS FailsFROM Table_1GROUP BY Level2[/code]
↧