Partially transpose a table in BigQuery SQL

31 Views Asked by At

I have a table that calculates revenues and costs like this :

Project name revenues costs margin
One 3 1 2
Two 10 3 7
Three 6 1 5

And need to transform the columns into additional rows, creating a unique column Money instead:

Project name type Money
One revenues 3
One costs 1
One margin 2
Two revenues 10
Two costs 3
Two margin 7
Three revenues 6
Three costs 1
Three margin 5

What type of transposition / Pivoting should I use?

1

There are 1 best solutions below

0
Mikhail Berlyant On BEST ANSWER

Use below approach

select Project_name, type, Money
from your_table
unpivot (Money for type in (revenues, costs, margin))

If applied to sample data in your question output is

enter image description here