案例陈述与案件陈述

时间:2015-07-22 13:56:01

标签: oracle oracle-sqldeveloper

我不确定我需要什么样的陈述才能完成我需要做的事情。我想在我目前的案例陈述之后添加一个案例陈述。贝娄是个案陈述。

CASE
    WHEN LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO')
    THEN 'GAS'
    WHEN LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC')
    THEN 'ELECTRIC'
    ELSE 'MISSING'
  END AS TYPE, 

我想说如果EXT_Distworktype是GC或GT并且TYPE是ELECTRIC, 如果EXT_DISTWORKTYPE为EC,ES或ET且Type为GAS,请给我一个类型为WRONG POLYGON的类型。

这是我的完整查询:

SELECT WO.WONUM AS "Work Order",
  WO.LOCATION   AS "Location",
  WO.STATUS     AS "Status",
  WO.DESCRIPTION,
  WO.ACTFINISH AS "Actual Finish",
  WO.PARENT    AS "Parent WO",
  WO.WORKTYPE  AS "Work Type",
  CASE
    WHEN WO.REPORTDATE IS NULL
    THEN TO_DATE('29-JAN-15 00:00:00', 'DD-MON-YY HH24:MI:SS')
    ELSE WO.REPORTDATE
  END       AS "Reported Date",
  WO.SITEID AS "Site",
  CASE
    WHEN WO.ACTFINISH IS NULL
    THEN GREATEST(TRUNC(TO_DATE(SysDate, 'DD-MON-YY HH24:MI:SS')      - TO_DATE(WO.REPORTDATE, 'DD-MON-YY HH24:MI:SS'), 6), 0)
    WHEN WO.STATUS NOT                                               IN ('COMP')
    THEN GREATEST(TRUNC(TO_DATE(SysDate, 'DD-MON-YY HH24:MI:SS')      - TO_DATE(WO.REPORTDATE, 'DD-MON-YY HH24:MI:SS'), 6), 0)
    WHEN WO.STATUS                                                   IN ('COMP')
    THEN GREATEST(TRUNC(TO_DATE(WO.ACTFINISH, 'DD-MON-YY HH24:MI:SS') - TO_DATE(WO.REPORTDATE, 'DD-MON-YY HH24:MI:SS'), 6), 0)
  END AS "Age",
  WO.EXT_DISTWORKTYPE,
  LOCOFF.EXT_OFFICE,
  CASE
    WHEN LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO')
    THEN 'GAS'
    WHEN LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC')
    THEN 'ELECTRIC'
    ELSE 'MISSING'
  END AS TYPE,
  LOCOFF.EXT_STATECODE,
  WO.OWNERGROUP,
  WO.EXT_JOBCODE
FROM LOCATIONS LOCOFF
RIGHT JOIN WORKORDER WO
ON WO.LOCATION       = LOCOFF.LOCATION
WHERE WO.STATUS NOT IN ('CLOSE', 'WAIV', 'CAN', 'REJ', 'REVOKED')
AND LOCOFF.SITEID    = 'OPS'
AND WO.EXT_JOBCODE NOT LIKE 'A%' AND WO.E`enter code here`XT_JOBCODE NOT LIKE 'B%'
AND WO.EXT_JOBCODE NOT IN ('K008','K009','I006','I007','I008');

2 个答案:

答案 0 :(得分:1)

如果您只想修改现有列值(而不是添加带有“错误多边形”消息的单独列),则可以嵌套案例表达式:

CASE
    WHEN LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO')
    THEN
        CASE
            WHEN WO.EXT_DISTWORKTYPE IN ('EC', 'ES', 'ET')
            THEN 'WRONG POLYGON'
            ELSE 'GAS'
        END
    WHEN LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC')
    THEN
        CASE
            WHEN WO.EXT_DISTWORKTYPE IN ('GC', 'GT')
            THEN 'WRONG POLYGON'
            ELSE 'ELECTRIC'
        END
    ELSE 'MISSING'
  END AS TYPE, 

快速SQL小提琴演示,使用相同的伪造数据; your original casethis nested case,其中包含:

EXT EX TYPE        
--- -- -------------
BOF GC GAS          
CDA GT GAS          
SPO ES WRONG POLYGON
CDC EC ELECTRIC     
COC ET ELECTRIC     
SPC GC WRONG POLYGON

这比尝试解释TYPE值的第二步更简单,因为您无法在同一级别的查询中引用列别名;您需要将现有查询转换为内联视图。

答案 1 :(得分:0)

额外的情况不会吗?

CASE
  WHEN WO.EXT_DISTWORKTYPE IN ('GC', 'GT') 
  AND LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC') --electronic
  THEN 'WRONG POLYGON'
CASE 
  WHEN WO.EXT_DISTWORKTYPE IN ('EC', 'ES', 'ET' ) 
  AND LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO') --gas
  THEN 'WRONG POLYGON'
ELSE 'MSSING'
END AS YOURANSWER,

您可以在case

中执行case
CASE 
  WHEN (TRUE) THEN
    CASE WHEN (true) THEN 'a' ELSE 'b' END
  ELSE 
     'A' 
END