我正试着整理一份报告,但我没能产生想要的结果。
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实现
1条答案
按热度按时间bwleehnv1#
首先必须准备数据,例如使用
rank()
,所以每个id
有编号的行:然后您可以制作pivot:
dbfiddle演示