[英]SQL: converting rows into columns
我目前有一個如下所示的表:
用戶身份 | 設備ID | 設備類型 |
---|---|---|
12_aa | 1245 | 移動的 |
15_ab | 8909 | 葯片 |
17歲 | 9090 | 移動的 |
18-ac | 8900 | 桌面 |
我正在嘗試將此表轉換為以下內容:
用戶身份 | 移動的 | 葯片 | 桌面 |
---|---|---|---|
12_aa | 1245 | 無效的 | 無效的 |
15_ab | 無效的 | 8909 | 無效的 |
17_ya | 9090 | 無效的 | 無效的 |
18_ac | 無效的 | 無效的 | 8900 |
你能幫助如何實現這一目標嗎?
您可以使用條件聚合:
select user_id,
max(case when device_type = 'mobile' then device_id end) as mobile,
max(case when device_type = 'tablet' then device_id end) as tablet,
max(case when device_type = 'desktop' then device_id end) as desktop
from t
group by user_id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.