Given two results consisting of single key|value pair (you can imagine those are in CTEs), I want to join and group them by key, aggregate their values and return two different things:
a) those keys where aggregated list of values in first resultset exactly matches aggregated list of values in second resultset
b) those keys where aggregated list of values in first resultset matches second resultset independent of order
Actually I know something very close to this is string_agg
, but It seems I can use this in select only and it's inefficient anyway. Is there something more efficient for this?
# resultset 1
|key | value |
|-----|-------|
| 1 | 1 |
| 1 | 2 |
| 3 | 4 |
| 2 | 5 |
| 2 | 7 |
| 1 | 3 |
# resultset 2
|key | value |
|-----|-------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 7 |
| 2 | 5 |
| 4 | 6 |
Desired result
a) key 1
(1,2,3 = 1,2,3)
b) key 1 and key 2
(5,7 = 7,5)