Just remove your join, it's redundant:
SELECT buildid,
SUM(CASE WHEN WindowsXP = 'PASS' THEN 1 ELSE 0 END) as PASS ,
SUM(CASE WHEN WindowsVista = 'FAIL' THEN 1 ELSE 0 END) as FAIL
FROM OS_Table
GROUP BY
buildId
This condition
WHERE BuildID IN (SELECT DISTINCT BuildID FROM OS_Table)
holds for any non-null buildid
in the table.
Update:
Your original query (with =
operator) meant this: "take all records from the table where buildId
equals to a single DISTINCT
value of buildId
taken from the same table, split them into several groups according to the value of buildID
and calculate the sums of the expressions within each group".
=
operator requires a scalar on both sides. In SQL
, a query is considered a scalar if and only if it returns a recordset of one field and at most one record.
Your subquery returned more that one record, so you original query failed (with quite a descriptive error).
With IN
operator, the query meant "take all records from the table where buildId
is found anywhere in the list of buildId
's taken from the same table, split them into several groups according to the value of buildID
and calculate the sums of the expressions within each group".
Since buildId
is always found the in the list of buildIs
taken from the same table, the condition is redundant.