Convert multiple rows with multiple column into one record in Oracle

ぃ、小莉子 提交于 2020-03-03 09:29:27

问题


In Oracle SQL query we got 40 records having 13 columns. I want to merge all these records into one column means 40 * 13 = 520 column in 1 record. Eg- Sample table having few records

col1  col2  city  cntry  conti
1     abc   NYC   USA    NA
2     def   LON   UK     EU
3     xyz   DUB   UAE    ASIA

then after merge all the records & get into the one record then it should be like the below one-

col1  col2  city  cntry  conti  col1  col2  city  cntry  conti  col1  col2  city  cntry  conti  
1     abc   NYC   USA    NA     2     def   LON   UK     EU     3     xyz   DUB   UAE    ASIA

回答1:


If column col1 contains unique values then you could use pivot:

select * 
  from t
  pivot (max(col1) col1, max(col2) col2, max(city) city, max(cntry), max(conti) conti 
         for col1 in (1, 2, 3))

SQLFiddle demo



来源:https://stackoverflow.com/questions/47993215/convert-multiple-rows-with-multiple-column-into-one-record-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!