BigQuery 中的透視轉換:將行轉置為列
問題:
目標是在BigQuery中將資料從行轉換為列,其中鍵變為列名稱和值成為對應的列值。
範例:
之前:
Key | Value |
---|---|
channel_title | Mahendra Guru |
youtube_id | ugEGMG4-MdA |
examId | 72975611-4a5e-11e5 |
channel_id | UCiDKcjKocimAO1tV |
postId | 1189e340-b08f |
之後:
channel_title | youtube_id | channel_id | examId | postId |
---|---|---|---|---|
Mahendra Guru | ugEGMG4-MdA | UCiDKcjKocimAO1tV | 72975611-4a5e-11e5 | 1189e340-b08f |
之後:
之後:解決方案:
SELECT 'SELECT id, ' + GROUP_CONCAT_UNQUOTED( 'MAX(IF(key = "' + key + '", value, NULL)) as [' + key + ']' ) + ' FROM yourTable GROUP BY id ORDER BY id' FROM ( SELECT key FROM yourTable GROUP BY key ORDER BY key )
目前的BigQuery缺乏本機旋轉功能。但是,以下方法可以達到相同的結果:
步驟 1:產生查詢結構SELECT id, MAX(IF(key = "channel_id", value, NULL)) AS [channel_id], MAX(IF(key = "channel_title", value, NULL)) AS [channel_title], MAX(IF(key = "examId", value, NULL)) AS [examId], MAX(IF(key = "postId", value, NULL)) AS [postId], MAX(IF(key = "youtube_id", value, NULL)) AS [youtube_id] FROM yourTable GROUP BY id ORDER BY id
以上是如何在 BigQuery 中將行轉換為列?的詳細內容。更多資訊請關注PHP中文網其他相關文章!