固定列上的sql数据透视

kyks70gy  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(341)

我正试着整理一份报告,但我没能产生想要的结果。

WITH DATA_PIVOT AS
(
SELECT 'GNA' SIGL, 'RC752293' ID_USER, '20200609' DATE_FILE, '11:30' HR_INTERVAL1,  10 DURATION1, '13:00' HR_INTERVAL2, 60 DURATION2,   '15:00' HR_INTERVAL3, 10 DURATION3, 'ENTRADA' OPERATION, TO_DATE('09/06/2020 11:35:21', 'DD/MM/YYYY HH24:MI:SS') TIME_INTERVAL, 'DESCANCO' TYPE_OF_INTERVAL FROM DUAL UNION ALL
SELECT 'GNA' , 'RC752293' ,     '20200609' , '11:30' ,  10 , '13:00' , 60 , '15:00' , 10 , 'SAIDA' , TO_DATE('09/06/2020 11:46:33', 'DD/MM/YYYY HH24:MI:SS') , 'DESCANCO'  FROM DUAL UNION ALL
SELECT 'GNA' , 'RC752293' ,     '20200609' , '11:30' ,  10 , '13:00' , 60 , '15:00' , 10 , 'ENTRADA' , TO_DATE('09/06/2020 15:48:04', 'DD/MM/YYYY HH24:MI:SS') , 'DESCANCO'  FROM DUAL UNION ALL
SELECT 'GNA' , 'RC752293' ,     '20200609' , '11:30' ,  10 , '13:00' , 60 , '15:00' , 10 , 'SAIDA' , TO_DATE('09/06/2020 16:01:44', 'DD/MM/YYYY HH24:MI:SS') , 'DESCANCO'  FROM DUAL
)

SELECT * FROM DATA_PIVOT;


在理想的情况下,我使用max(decode())解决了这个问题,并为“id\u user”创建了以下记录序列,但我有几个缺少列“type\u of \u interval”的记录(如上图所示)。在一个完美的世界里:

WITH DATA_PIVOT AS
(
SELECT 'GNA' SIGL, 'RC752293' ID_USER, '20200609' DATE_FILE, '11:30' HR_INTERVAL1,  10 DURATION1, '13:00' HR_INTERVAL2, 60 DURATION2,   '15:00' HR_INTERVAL3, 10 DURATION3, 'ENTRADA' OPERATION, TO_DATE('09/06/2020 11:35:21', 'DD/MM/YYYY HH24:MI:SS') TIME_INTERVAL, 'DESCANCO' TYPE_OF_INTERVAL FROM DUAL UNION ALL
SELECT 'GNA' , 'RC752293' ,     '20200609' , '11:30' ,  10 , '13:00' , 60 , '15:00' , 10 , 'SAIDA' , TO_DATE('09/06/2020 11:46:33', 'DD/MM/YYYY HH24:MI:SS') , 'DESCANCO'  FROM DUAL UNION ALL
SELECT 'GNA' , 'RC752293' ,     '20200609' , '11:30' ,  10 , '13:00' , 60 , '15:00' , 10 , 'ENTRADA' , TO_DATE('09/06/2020 15:48:04', 'DD/MM/YYYY HH24:MI:SS') , 'LANCHE'  FROM DUAL UNION ALL
SELECT 'GNA' , 'RC752293' ,     '20200609' , '11:30' ,  10 , '13:00' , 60 , '15:00' , 10 , 'SAIDA' , TO_DATE('09/06/2020 16:01:44', 'DD/MM/YYYY HH24:MI:SS') , 'LANCHE'  FROM DUAL UNION ALL
SELECT 'GNA' , 'RC752293' ,     '20200609' , '11:30' ,  10 , '13:00' , 60 , '15:00' , 10 , 'ENTRADA' , TO_DATE('09/06/2020 17:48:04', 'DD/MM/YYYY HH24:MI:SS') , 'DESCANCO'  FROM DUAL UNION ALL
SELECT 'GNA' , 'RC752293' ,     '20200609' , '11:30' ,  10 , '13:00' , 60 , '15:00' , 10 , 'SAIDA' , TO_DATE('09/06/2020 18:01:34', 'DD/MM/YYYY HH24:MI:SS') , 'DESCANCO'  FROM DUAL
)
SELECT * FROM DATA_PIVOT;


在理想的情况下,我使用max(decode())解决了这个问题,并为“id\u user”创建了以下记录序列,但我有几个缺少列“type\u of \u interval”的记录(如上图所示)。在一个完美的世界里:
但是,当类型\的\间隔列中缺少记录时,我无法格式化相应列中的类型。如何使用oracle 12c版本的sql生成输出?我完全迷路了,我不知道还能去哪里,我尝试了几个pivot实现

bwleehnv

bwleehnv1#

首先必须准备数据,例如使用 rank() ,所以每个 id 有编号的行:

select data_pivot.*, 
       rank() over (partition by id_user, operation, type_of_interval 
                    order by time_interval) rn
  from data_pivot

然后您可以制作pivot:
dbfiddle演示

select * 
  from (select data_pivot.*, 
               rank() over (partition by id_user, operation, type_of_interval 
                            order by time_interval) rn 
          from data_pivot) 
  pivot (max(time_interval) for (rn, operation, type_of_interval) in 
    ( (1, 'ENTRADA', 'DESCANCO') init1,
      (1, 'SAIDA',   'DESCANCO') end1,
      (1, 'ENTRADA', 'LANCHE')   init2,
      (1, 'SAIDA',   'LANCHE')   end2,
      (2, 'ENTRADA', 'DESCANCO') init3,
      (2, 'SAIDA',   'DESCANCO') end3
    ) )

相关问题