I have a query which returns a result set which looks like this:
A | B
---------------------
1 | a
2 | a
1 | b
1 | c
3 | d
A|B is unique in the results (i.e. there will only ever be one 1|a row)
I need to write two queries based on these results. The first:
given a value for A (say 1), count the number of Bs who only have a single row; with a 1 in the A column.
so in the above data set, id expect to be returned the value 2, because B values b and c only have one row each, where those rows have a 1 in the A column. d would not be counted because it corresponds to a 3 and a would not be returned because it has an additional row with a 2.
Hope that makes sense.
The second query is the opposite to the above.
Given a value for A (say 1) count the number of Bs who have 2 or more rows, where one of the rows has a 1 in the A column.
So in the above data id expect to be returned 1, because the B value a is the only one that has multiple rows, with one of them with a 1 in the A column.
Thanks
With CTEs and windowed aggregate functions, it could be done like this: