简体   繁体   中英

Making multiple columns into a row in bigquery

I have a table with below data in bigqyery

Qualification_No 2019.2 2019.1 2018.4
100/1802/5        10     20    30
100/1811/6        15     23    10

I want the data to be like below

Qualification_No No_of_certification
100/1802/5         10
100/1802/5         20
100/1802/5         30
100/1811/6         15
100/1811/6         23
100/1811/6         10

Please help to write a query for this.

You need to use UNION ALL

select `Qualification_No`, `2019.2` as no from table
union all
select `Qualification_No`, `2019.1` as no from table
union all
select `Qualification_No`, `2018.4` as no from table

Below is for BigQuery Standard SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '100/1802/5' Qualification_No, 10 col_2019_2, 20 col_2019_1, 30 col_2018_4 UNION ALL
  SELECT '100/1811/6', 15, 23, 10
)
SELECT Qualification_No, No_of_certification
FROM `project.dataset.table`,
UNNEST([col_2019_2, col_2019_1, col_2018_4]) No_of_certification

with result

Row Qualification_No    No_of_certification  
1   100/1802/5          10   
2   100/1802/5          20   
3   100/1802/5          30   
4   100/1811/6          15   
5   100/1811/6          23   
6   100/1811/6          10   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM