我需要使用oracle在以下使用CASE语句创建的列中填充“ DAYTIME”值的帮助。
我的脚本
WITH your_query
AS (SELECT SLMCU AS "BUSINESSUNIT",
SLDOCO AS "ORDERNO",
SLDCTO AS "ORDERTYPE",
SLAN8 AS "CUSTOMERNO",
ABALPH AS "CUSTOMERNAME",
SLLITM AS "ITEMNO",
SLDSC1 AS "DESCRIPTION",
SLDSC2 AS "DESCRIPTION2",
SLLTTR AS "LAST STATUS",
SLNXTR AS "NEXT STATUS",
CASE SLTRDJ
WHEN 0 THEN TO_DATE (TO_CHAR (1 + 1900000), 'YYYYDDD')
ELSE TO_DATE (TO_CHAR (SLTRDJ + 1900000), 'YYYYDDD')
END
AS "ORDER DATE",
CASE SLADDJ
WHEN 0 THEN TO_DATE (TO_CHAR (1 + 1900000), 'YYYYDDD')
ELSE TO_DATE (TO_CHAR (SLADDJ + 1900000), 'YYYYDDD')
END
AS "SHIPPED DATE",
SLTDAY AS "DAYTIME",
SLUORG / 10000 AS "ORDER QUANTITY",
SLSOQS / 10000 AS "SHIPPED QUANTITY"
FROM PRODDTA.F42199 INNER JOIN PRODDTA.F0101 ON SLAN8 = ABAN8
WHERE SLTRDJ = 119327 AND SLLNID = 1000 AND ROWNUM <= 200)
SELECT *
FROM (SELECT t.*,
CASE
WHEN "NEXT STATUS" = 540 THEN "DAYTIME"
END
AS time_of_day,
CASE
WHEN "NEXT STATUS" = 580
THEN
"DAYTIME"
END
AS shipped_time
FROM (SELECT q.*
FROM your_query q) t
WHERE "NEXT STATUS" IN (540, 580, 620))
WHERE "NEXT STATUS" = 620
结果
我要实现的是,当满足“ NEXT STATUS”条件但查询失败时,从内部查询返回的每一行的“ DAYTIME”值,从“ time_of_day”中的空值判断“ shipped_time”列。
所需的输出
编辑:数据模型
ORDER_NO ORDER_TYPE CUSTOMER_NO CUSTOMER_NAME LAST_ STATUS NEXT_ STATUS ORDER_ DATE SHIPPED_DATE DAYTIME ORDER_ QTY SHIPPED_ QTY 19440263 SO 295042 MERU DEPOT 520540 12/5/2019 1/1/1900 140556 8 8 19440263 SO 295042 MERU DEPOT 540580 12/5/2019 12/5/2019 140745 8 8 19440263 SO 295042 MERU DEPOT 580582 12/5/2019 12/5/2019 140822 8 8 19440263 SO 295042 MERU DEPOT 582620 12/5/2019 12/5/2019 140837 8 8 19440263 SO 295042 MERU DEPOT 620999 12/5/2019 12/5/2019 90333 8 8
19440270 SO 295042 MERU仓库520540 12/5/2019 1/1/1900 144529 2 2 19440270 SO 295042 MERU DEPOT 540580 12/5/2019 12/5/2019 144725 2 2 19440270 SO 295042 MERU DEPOT 580582 12/5/2019 12/5/2019 144757 2 2 19440270 SO 295042 MERU DEPOT 582620 12/5/2019 12/5/2019 144813 2 2 19440270 SO 295042 MERU DEPOT 620999 12/5/2019 12/5/2019 90336 2 2
19562535 ST 295046 NAKURU仓库520540 12/5/2019 1/1/1900 202705 11 11 19562535 ST 295046 NAKURU DEPOT 540560 12/5/2019 1/1/1900 203058 11 11 19562535 ST 295046 NAKURU DEPOT 560580 12/5/2019 12/5/2019 233630 11 11 19562535 ST 295046 NAKURU DEPOT 580582 12/5/2019 12/5/2019 234505 11 11 19562535 ST 295046 NAKURU DEPOT 582620 12/5/2019 12/5/2019 234627 11 11
19759237 SV 200977 D HOLDINGS LTD 520540 12/5/2019 1/1/1900 134200 12 12 19759237 SV 200977 D HOLDINGS LTD 540560 12/5/2019 1/1/1900 134800 12 12 19759237 SV 200977 D HOLDINGS LTD 560580 12/5/2019 12/5/2019 160704 12 12 19759237 SV 200977 D HOLDINGS LTD 580582 12/5/2019 12/5/2019 160911 12 12 19759237 SV 200977 D HOLDINGS LTD 582620 12/5/2019 12/5/2019 161034 12 12 19759237 SV 200977 D HOLDINGS LTD 620999 12/5/2019 12/5/2019 90340 12 12
19759240 SV 200007 Naivasha Gen 520540 12/5/2019 1/1/1900 155110 11 11 19759240 SV 200007 Naivasha Gen 540560 12/5/2019 1/1/1900 155506 11 11 19759240 SV 200007 Naivasha Gen 560580 12/5/2019 12/5/2019 173205 11 11 19759240 SV 200007 Naivasha Gen 580582 12/5/2019 12/5/2019 173424 11 11 19759240 SV 200007 Naivasha Gen 582620 12/5/2019 12/5/2019 173526 11 11 19759240 SV 200007 Naivasha Gen 620999 12/5/2019 12/5/2019 90346 11 11
答案 0 :(得分:0)
基于您的原始查询和注释,您似乎想要输出子查询中具有两个派生列的所有列; TIME_OF_DAY和SHIPPED。 在您的查询中,这两列都包含子查询中的DAYTIME值。 因此,以下更简单的查询应满足那些要求。您还只希望NEXT TATUS为620的记录(540和580似乎根本不相关)。
WITH your_query
AS (...your original sub-query...)
SELECT q.*,
"DAYTIME" AS time_of_day,
"DAYTIME" AS shipped_time
FROM your_query q
WHERE "NEXT STATUS" = 620
编辑: 因此,这与您最初想要的需求有很大不同。 该查询将3个slnxtr值分解为它们自己的子查询,然后将它们联接。 在无法正确了解您的数据模型/数据的情况下,我无法确定联接条件,但从概念上讲,这应该是最简单的。 可能还有其他方法可以使用解析函数,但是又一次,我不知道您不希望推测出您的状态转换规则。
WITH row_620
AS (SELECT SLMCU AS "BUSINESSUNIT",
SLDOCO AS "ORDERNO",
SLDCTO AS "ORDERTYPE",
SLAN8 AS "CUSTOMERNO",
ABALPH AS "CUSTOMERNAME",
SLLITM AS "ITEMNO",
SLDSC1 AS "DESCRIPTION",
SLDSC2 AS "DESCRIPTION2",
SLLTTR AS "LAST STATUS",
SLNXTR AS "NEXT STATUS",
CASE SLTRDJ
WHEN 0 THEN TO_DATE (TO_CHAR (1 + 1900000), 'YYYYDDD')
ELSE TO_DATE (TO_CHAR (SLTRDJ + 1900000), 'YYYYDDD')
END AS "ORDER DATE",
CASE SLADDJ
WHEN 0 THEN TO_DATE (TO_CHAR (1 + 1900000), 'YYYYDDD')
ELSE TO_DATE (TO_CHAR (SLADDJ + 1900000), 'YYYYDDD')
END AS "SHIPPED DATE",
SLTDAY AS "DAYTIME",
SLUORG / 10000 AS "ORDER QUANTITY",
SLSOQS / 10000 AS "SHIPPED QUANTITY"
FROM PRODDTA.F42199 INNER JOIN PRODDTA.F0101 ON SLAN8 = ABAN8
WHERE SLTRDJ = 119327 AND SLLNID = 1000 AND ROWNUM <= 200
AND SLNXTR = 620)
row_540
AS (SELECT sldoco, sltday
FROM proddta.f42199
INNER JOIN PRODDTA.F0101 ON SLAN8 = ABAN8
WHERE SLTRDJ = 119327 AND SLLNID = 1000
AND SLNXTR = 540)
row_580
AS (SELECT sldoco, sltday
FROM proddta.f42199
INNER JOIN PRODDTA.F0101 ON SLAN8 = ABAN8
WHERE SLTRDJ = 119327 AND SLLNID = 1000
AND SLNXTR = 580)
SELECT t.*,
b.sltday time_of_day,
c.sltday shipped_time
FROM row_640 a
LEFT OUTER JOIN
row_540 b ON a.sldoco = b.sldoco
LEFT OUTER JOIN
row_580 c ON a.sldoco = c.sldoco