Oracle中将多行多列转换为一条记录

问题描述:

在 Oracle SQL 查询中,我们得到 40 条记录,其中 13 列.我想将所有这些记录合并一列意味着1条记录中的40 * 13 = 520列.例如- 具有很少记录的样本表

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

如果 col1 列包含唯一值,那么您可以使用 pivot:

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 演示