newToScala
newToScala

Reputation: 407

Group by the union of two columns

How can GROUP BY based on the union of two columns be achieved performantly? There may be NULL values in either column. Something like (obviously this doesn't work):

SELECT a.val, b.val
FROM a 
LEFT JOIN b on a.id = b.id
GROUP BY UNION(a.val, b.val)

With results like:

a.val | b.val
-----------
1        1
2        2
NULL     3
4        NULL
5        5

Thanks!

Upvotes: 0

Views: 298

Answers (1)

Rams
Rams

Reputation: 2169

Why can't you use NVL

SELECT NVL(a.val, b.val)  FROM a LEFT JOIN b on a.id = b.id 
GROUP BY NVL(a.val, b.val)

Upvotes: 1

Related Questions