Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
308 views
Welcome To Ask or Share your Answers For Others

1 Answer

I'm sure you can work this out yourself.. I guess it needs to look something like this.. but you don't say how you want the data to be combined so I've just just a cross product (the WHERE 1=1) of the last data set to a union of the first two

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...