I need to perform a group by, but only on rows that meet a condition, otherwise return all rows that do not meet the condition. For example, in the following table, I want to group only rows that have ‘1’ in the “active” field, and return all rows that do not.
TABLE (id, label, active):
1, A, 1
2, A, 1
3, B, 0
4, B, 0
Would return:
1, A, 1
3, B, 0
4, B, 0
Here is the simplest way I can think of. It is a case statement in the group by. If the condition is met then group by the label, otherwise group by the primary key.
If you want to group by active and label:
EDIT: I misunderstood which field you want to group on. Corrected now.