需要数据

时间:2018-10-09 23:43:07

标签: sql oracle pivot

我在这里的表中有数据

i have data in a table in the format below

我正在尝试将其转换为这种格式/所需的输出

out put formate

非常感谢您的帮助

2 个答案:

答案 0 :(得分:1)

这不是关系结构,因为行中的列实际上并不相互关联。因此,通常最好在应用程序级别完成这种类型的转换。

也就是说,您可以在Oracle中执行此操作。一种方法是条件聚合:

select col1,
       max(case when col3 = 'CAT1' then col2 end) as cat1,
       max(case when col3 = 'CAT2' then col2 end) as cat2
from (select t.*, row_number() over (partition by col1, col3 order by col2) as seqnum
      from t
     ) t
group by col1, seqnum
order by col1, seqnum;

答案 1 :(得分:1)

您可以使用PIVOT:

select col1, cat1, cat2
from (
  select t.*, row_number() over (partition by col3 order by col2) as rn
  from t
)
pivot (min(col2) for (col3) in ('CAT1' as "CAT1", 'CAT2' as "CAT2"))
order by rn;

enter image description here

LiveSQL