T-SQL Count rows with specific values (Multiple in one query)
SELECT IGrp,
COUNT(CASE WHEN Value1 > 1 THEN 1 ELSE NULL END) AS V1High,
COUNT(CASE WHEN Value2 > 1 THEN 1 ELSE NULL END) AS V2High
FROM Tbl
GROUP BY IGrp
You can use the CASE
statement:
SELECT IGrp,
SUM(CASE WHEN Value1>1 THEN 1 ELSE 0 END) AS V1High,
SUM(CASE WHEN Value2>1 THEN 1 ELSE 0 END) AS V2High
FROM Tbl GROUP BY IGrp