我有以下的數據集(樣本):MySQL的 - 重塑數據
emplid | Citizeship |
100001 | USA |
100001 | CAN |
100001 | CHN |
100002 | USA |
100002 | CHN |
100003 | USA |
有沒有辦法把數據轉化成如下:
emplid | Citizeship_1 | Citizenship_2 | Citizenship_3
100001 | USA | CHN | CAN
100002 | USA | CHN |
100003 | USA | |
的假設是,各個emplid
會有多達4個國籍。
我開始用下面的代碼,但對於emplid
小號誰只是有1 citizenship
,正在重演在citizenship_2
,citizenship_3
價值,這應該只是空白:
select *
, substring_index(Citizenship_multiple, ',', 1) as Citizenship_1
, substring_index(substring_index(Citizenship_multiple,',',-1),',',1) as Citizenship_2
, substring_index(substring_index(Citizenship_multiple,',',-2),',',1) as Citizenship_3
, substring_index(substring_index(Citizenship_multiple,',',-3),',',1) as Citizenship_4
from
(select *
, group_concat(distinct Citizenship) as Citizenship_multiple
from `citizenship_csv_meta`
group by emplid) a
感謝,但我給的數據只是一個小樣本,真正的數據集將有超過170個國家,所以我不認爲硬編碼國家是有效的:( – PMa 2014-11-04 23:16:45