I'm building a query with a GROUP BY clause that needs the ability to count records based only on a certain condition (field value is equal to 1).
SELECT
UID, COUNT(UID) AS TotalRecords, SUM(ContractDollars) AS ContractDollars,
--- Get the average of all records that satisfy the condition.
(COUNTIF(MyColumn, 1) / COUNT(UID) * 100) are 1
FROM
dbo.AD_CurrentView
GROUP BY
UID
HAVING
SUM(ContractDollars) >= 500000
The COUNTIF() line obviously fails since there is no native SQL function called COUNTIF(), but the idea here is to determine the percentage of all rows that have the value '1' for MyColumn.
Any thoughts on how to properly implement this in a MS SQL 2005 environment?