Skip to content
Advertisement

T-SQL – subqueries on rows grouped by ID to create a summary table

I have a table “MyTable” with an id and two int fields “A” and “B”:

I’d like a T-SQL query to return a single row for each distinct id, which contains:

each distinct ID
whether there exists a row for that ID with a non-zero value for “a”
whether there exists a row for that ID with a non-zero value for “b”

like so:

I can GROUP BY the ID, but I don’t know how to create the joins or subqueries on each group to get the desired result.

Advertisement

Answer

Or you can just use since your value is 1 and 0. But if value is other than that please use first query.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement