I have three queries and i have to combine them together.
Query 1
WITH ph
AS (SELECT chrd, chwo, chse, chst, chvr, chfv, chrd,
ROW_NUMBER () OVER(PARTITION BY chwo ORDER BY chse, chvr desc) TEMP
FROM wrpd.wscl
WHERE chaj > '20180901'
AND chst = 'R'
AND chstb IN ( 'L1', 'R2' )
ORDER BY chse)
SELECT *
FROM ph A
WHERE A.temp = 1
Query 2
SELECT chrd, chwo, chse, chst, chvr, chfv, chrd,
ROW_NUMBER () OVER( PARTITION BY chwo ORDER BY chse, chvr desc) TEMP
FROM wrpd.wscl
WHERE chajdt > '20180901'
AND chst IN ( 'P', 'A' )
AND chstb IN ( 'L1', 'R2' )
ORDER BY chst desc, chse
Query 3
SELECT partd1, actdd1, dmre, dmde, dlro, dord, wvin, rcdt,
CHAR(DATE(Substr(CHAR(rcdt), 1, 4)
|| '-'
|| Substr(CHAR(rcdt), 5, 2)
|| '-'
|| Substr(CHAR(rcdt), 7, 2)), usa) AS "Conversion",
SUM(qty) AS Shipments,
rcdt-dord AS Ship_Days
FROM pspd.zpslsp,
pspd.zpslma,
dlpd.drdm,
wrpd.wscl
WHERE partd1 = partpm
AND delr = dmde
AND actdd1 BETWEEN '201801' AND '201810'
AND otypd1 NOT IN ( 'T', 'Z', 'W' )
AND dmty = 'RD'
AND partpm LIKE '21101%'
AND Substr(delr, 3, 3) NOT BETWEEN '390' AND '399'
AND qtys > 0
AND chfv = wvin
GROUP BY partd1, actdd1, dmde, dlro, dord, rcdt, dmre, wvin
ORDER BY 1, 2
Desired columns in final result :
DMRE, CHRD, DMDE, CHWO, CHSE, CHST, CHVR, CHFV, WVIN, PARTd1, ACTDd1,
CHRD, DLRO, DORD, RCDT,
CHAR(DATE(Substr(CHAR(rcdt), 1, 4)
|| '-'
|| Substr(CHAR(rcdt), 5, 2)
|| '-'
|| Substr(CHAR(rcdt), 7, 2)), usa)
AS "Conversion",
SUM(QTYS) AS Shipments, RCDT-DORD AS Ship_Days
ROW_NUMBER () OVER(PARTITION BY CHWO ORDER BY CHSE,CHVR DESC) TEMP
Note: I can't combine query 1 and query 2 as single one, as the output is different.
New Edit: I added "ROW_NUMBER () OVER(PARTITION BY CHWO ORDER BY CHSE,CHVR DESC) TEMP
" to query 2 which might help to use union.
Thanks in advance!
See Question&Answers more detail:
os