Skip to content
Advertisement

How to transpose the vertical data into horizontal by using BigQuery?

I am currently working on the employees’ benefits data. However, the spreadsheet data is totally in a mess. I would like to format it as easy to capture the information. The current formatting is as below:

I would like to transfer it like this:

I tried grouping the data by names and BenefitCode, but I just got so confused about it.

My codes are as below:

But it seems like not a good idea to lose the dependents’ relationship to the employee. Can anyone tell me how to transfer the vertical data into horizontal? Or do you have any good idea to solve it?

Advertisement

Answer

Below is for BigQuery Standard SQL

If to apply to sample data from your question – result is

Another option would be extend above flatten version into “hierarchical”

In this case, result will be

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