我想用 BigQuery 做這樣的事情。輸入表
| Col1 | Col2 | Col3 | Col4 |
|---|---|---|---|
| 1 | A,B,C | 123 | 789 |
輸出表
| ID | 科爾 | 價值 |
|---|---|---|
| 1 | COL1 | 1 |
| 1 | COL2 | A,B,C |
| 1 | COL3 | 123 |
| 1 | COL4 | 789 |
以橫向視圖爆炸(地圖。
但我不能在 bigquery
uj5u.com熱心網友回復:
考慮以下方法
select id, col, value
from (select *, row_number() over() as id from your_table)
unpivot (value for col in (Col1, Col2, Col3, Col4))
f 適用于您問題中的樣本資料
with your_table as (
select '1' Col1, 'A,B,C' Col2, '123' Col3, '789' Col4
)
輸出是

注意 - 這種特殊方法要求所有列 (Col1 - Col4) 的型別相同。如果不是這種情況,您將需要首先為其中一些應用強制轉換以使其成為字串
uj5u.com熱心網友回復:
如果它是離散數量的列,則可以為此使用 UNION ...
select id, 'Col1' as Column, col1 as Value
from table
union all
select id, 'Col2' as Column, col2 as Value
from table
union all
select id, 'Col3' as Column, col3 as Value
from table
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418723.html
標籤:
