I currently have a table that looks like the following:
user_id | device_id | device_type |
---|---|---|
12_aa | 1245 | mobile |
15_ab | 8909 | tablet |
17-ya | 9090 | mobile |
18-ac | 8900 | desktop |
I am trying to convert this table to below:
user_id | mobile | tablet | desktop |
---|---|---|---|
12_aa | 1245 | null | null |
15_ab | null | 8909 | null |
17_ya | 9090 | null | null |
18_ac | null | null | 8900 |
Could you help how this can be achieved?
You can use conditional aggregation:
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;
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.