Sql Query(Oracle DB);
select DT.MODULENO,DT.SLOTNO,DT.DID,MAX(DT.DEVICEKEY)as TIME
from DEVICETRACE DT
INNER JOIN PCBTRACE PT on DT.DEVICEKEY=PT.DEVICEKEY
where SUBSTR(PT.PCBID,7,6)='019479'
group by DT.MODULENO,DT.SLOTNO,DT.DID
MDN SLNO DID TIME
1 1 3017230000001705 20171003051515
1 9 3017260000001437 20171002225453
1 9 3017260000001438 20171003051515
1 16 3017230000001681 20171002235012
1 16 3017230000001680 20171003051515
1 17 3017270000003284 20171003074307
1 17 3017270000003249 20171003145129
1 17 3017270000003253 20171003153600
1 18 3017240000000274 20171003153600
1 19 3017200000000148 20171003083715
1 19 3017200000000147 20171003120846
1 19 3017200000000146 20171003153420
1 19 3017200000000145 20171003153600
1 20 3017230000000876 20171003051515
1 21 3017250000001389 20171003072136
1 21 3017250000001388 20171003082830
1 21 3017250000001395 20171003093001
1 21 3017250000001408 20171003103917
如何执行以下结果。
MD SLNO BEFORE_DID NOW_DID TIME
1 1 - 3017230000001705 20171003051515
1 1 3017260000001437 3017260000001438 20171003051515
1 16 3017230000001681 3017230000001680 20171003051515
1 17 3017270000003249 3017270000003253 20171003145129
1 19 3017200000000147 3017200000000146 20171003153420
1 18 - 3017240000000274 20171003153600
我在上面的示例中想要的是将最后两个添加的MDNO和SLNO组的DID并排打印为两列。查询如何获取上面的代码,如上例所示?致谢
答案 0 :(得分:0)
类似的东西:
WITH your_query ( MDN, SLNO, DID, time ) AS (
-- your query here
)
SELECT *
FROM ( SELECT MDN,
SLNO,
DID,
MAX( time ) OVER ( PARTITION BY MDN, SLNO ) AS time,
ROW_NUMBER() OVER ( PARTITION BY MDN, SLNO ORDER BY time DESC ) AS rn
FROM your_query )
PIVOT ( MAX( DID ) FOR rn IN ( 2 AS Before_DID, 1 AS Now_DID ) );