Skip to content
Advertisement

SQL GROUP BY columns with inverted values

Suppose I’ve a table with like this:

Where G1, G3 are VARCHAR and G2, G4 are INT

If I do a simple GROUP BY on G1..G4 I get:

I’m wondering if is possible to aggregate on inverted value G1,G2 <-> G3,G4. What I want to achieve is a result like this:

What I tried is to get the inverted rows, but I still can’t aggregate on them due to repetition. My test query is:

Advertisement

Answer

If I’ve got it right, reverse the order of columns so that A B C D matches C D A B .

db<>fiddle

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