Skip to content
Advertisement

Using the Union function in SQL to reverse columns

I have the following SQL code to produce the following table of data:

I am hoping to transform data to create a table that looks like:

What would be the best way to achieve this? Something like creating two tables and then a Union join?

Advertisement

Answer

You can use self join to achieve what are you looking for.

Schema:

Query:

Output:

team opposition Shots Shots_on_Target Goals Time Day oppo_shots Oppo_Shots_on_Target opp_Goals
Brazil England 6 3 1 1200 Saturday 3 1 0
England Brazil 3 1 0 1200 Saturday 6 3 1

db<>fiddle here

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