Oracle Query基于数据的水平

我想在下面的数据样本中查询这些。 (Oracle数据库)

表格名称:FL_TEST

NAMA    JAM  STATUS
A       2    P
A       1    O
A       3    O
B       1    O
B       2    P
B       3    O
C       1    O
C       2    P
C       3    P

要这样:

NAMA    JAM1 JAM2 JAM3
A       O    P    O
B       O    P    O
C       O    P    P

是否可以执行该查询?

qjp1970 回答:Oracle Query基于数据的水平

使用有条件的聚集地

select NAMA,max(case when jam=1 then status end) as jam1,max(case when jam=2 then status end) as jam2,max(case when jam=3 then status end) as jam3
from FL_TEST group by NAMA

或者您可以使用数据透视

SELECT *
  FROM FL_TEST
PIVOT (
  MAX(status) as JAM FOR jam IN (1,2,3)
 );
,

您可以使用Conditional Aggregation

select nama,max( case when jam = 1 then status end ) as jam1,max( case when jam = 2 then status end ) as jam2,max( case when jam = 3 then status end ) as jam3
  from tab
 group by nama

pivot

select * 
  from tab
 pivot(
       max(value) for field in ( 1 as "Jam1",2 as "Jam2",3 as "Jam3" ) )

Demo

本文链接:https://www.f2er.com/3161511.html

大家都在问