我有三个查询,我必须将它们组合在一起。
查询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
查询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
查询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
最终结果中所需的列:
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
注意:由于输出不同,我无法将查询1和查询2合并为一个。
新编辑:我在查询2中添加了“ ROW_NUMBER () OVER(PARTITION BY CHWO ORDER BY CHSE,CHVR DESC) TEMP
”,这可能有助于使用并集。
谢谢!
答案 0 :(得分:0)
我确信您可以自己解决这个问题。我想它看起来应该像这样。但是您没有说要如何合并数据,因此我只是一个交叉产品(最后一个数据集的WHERE 1=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
), DT AS(
SELECT CHRD,CHWO,CHSE,CHST,CHVR,CHFV,CHRD,
ROW_NUMBER () OVER(PARTITION BY CHWO ORDER BY CHSE,CHVR DESC) TEMP
FROM WRPDAT.WSCLHP
WHERE CHAJDT > '20180901'
AND CHST IN ('P','A')
AND CHSTB in ('L1', 'R2')
)
, U AS (
SELECT *
FROM PH A
WHERE A.TEMP=1
UNION ALL
SELECT *
FROM DT
)
, Z AS
(
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
)
SELECT U.*, Z.* FROM U, Z WHERE CHFV = WVIN
ORDER BY TEMP, PARTd1, ACTDd1