在函数中使用Case语句

时间:2016-02-05 15:02:21

标签: sql db2

我正在处理我正在编写的一个查询问题 - 我正在创建一个指标字段,如果满足几个案例,我只想显示为“1”。该字段是使用MAX()函数内的case语句构建的 - 我需要该函数,因为该部分是CTE的一部分,该CTE与另一个将行转换为列的CTE连接,并且我使用函数进行横向化。

 MAX(CASE
     WHEN PMOD1_PGM IN ('IR') AND INTEGER(PMOD1_PCT) <> 0
       THEN 1
     WHEN PMOD2_PGM IN ('IR') AND INTEGER(PMOD2_PCT) <> 0
       THEN 1
     WHEN PMOD3_PGM IN ('IR') AND INTEGER(PMOD3_PCT) <> 0
       THEN 1
     WHEN PMOD4_PGM IN ('IR') AND INTEGER(PMOD4_PCT) <> 0
       THEN 1
     WHEN PMOD5_PGM IN ('IR') AND INTEGER(PMOD5_PCT) <> 0
       THEN 1
     WHEN PMOD6_PGM IN ('IR') AND INTEGER(PMOD6_PCT) <> 0
       THEN 1
     ELSE 0 END) AS @IRPM_IND,
      /*@IRPM_PCT*/
 MAX(CASE
     WHEN PMOD1_PGM IN ('IR') AND PMOD1_CD IN ('C')
       THEN PMOD1_PCT * - 1
     WHEN PMOD1_PGM IN ('IR') AND PMOD1_CD IN ('D')
       THEN PMOD1_PCT
     WHEN PMOD2_PGM IN ('IR') AND PMOD1_CD IN ('C')
       THEN PMOD2_PCT * - 1
     WHEN PMOD2_PGM IN ('IR') AND PMOD1_CD IN ('D')
       THEN PMOD2_PCT
     WHEN PMOD3_PGM IN ('IR') AND PMOD1_CD IN ('C')
       THEN PMOD3_PCT * - 1
     WHEN PMOD3_PGM IN ('IR') AND PMOD1_CD IN ('D')
       THEN PMOD3_PCT
     WHEN PMOD4_PGM IN ('IR') AND PMOD1_CD IN ('C')
       THEN PMOD4_PCT * - 1
     WHEN PMOD4_PGM IN ('IR') AND PMOD1_CD IN ('D')
       THEN PMOD4_PCT
     WHEN PMOD5_PGM IN ('IR') AND PMOD1_CD IN ('C')
       THEN PMOD5_PCT * - 1
     WHEN PMOD5_PGM IN ('IR') AND PMOD1_CD IN ('D')
       THEN PMOD5_PCT
     WHEN PMOD6_PGM IN ('IR') AND PMOD1_CD IN ('C')
       THEN PMOD6_PCT * - 1
     WHEN PMOD6_PGM IN ('IR') AND PMOD1_CD IN ('C')
       THEN PMOD6_PCT
     ELSE 0 END) AS @IRPM_PCT

基本上我检查一个字段以查看是否存在'IR'并且其百分比不等于0-如果是这样我想要一个指示符1.但是当我运行它时,仍然有一些条目通过指标1和@IRPM_PCT为0.任何建议?它是否与MAX()函数中的case语句有关?当我检查通过的数据时,它似乎“工作”但我想消除这些指标1,百分之0的案例,我认为我建立指标案例陈述的方式将处理这个。

1 个答案:

答案 0 :(得分:1)

在IRPM_PCT CASE语句中,您将PMOD6_PGM检查为“C”两次(而不是“C”,然后是“D”)。

因此,如果PMOD6_PGM ='IR',则PMOD6_PCT非零,且PMOD1_CD ='D', 然后你会得到IRPM_IND = 1和IRPM_PCT = 0(因为016-02-05 11:44:00 WARN DFSClient:975 - Slow ReadProcessor read fields took 30 011ms (threshold=30000ms); ack: seqno: 1960 reply: 0 reply: 0 reply: 0 downstrea mAckTimeNanos: 1227280, targets: [DatanodeInfoWithStorage[10.0.0.245:50010,DS-a5 5d9212-3771-4936-bbe7-02035e7de148,DISK], DatanodeInfoWithStorage[10.0.0.243:500 10,DS-231b9915-c2e2-4392-b075-8a52ba1820ac,DISK], DatanodeInfoWithStorage[10.0.0 .244:50010,DS-6b8b5814-7dd7-4315-847c-b73bd375af0e,DISK]] 2016-02-05 11:44:00 INFO BlockManager:59 - Removing RDD 1954 2016-02-05 11:44:00 INFO MapPartitionsRDD:59 - Removing RDD 1955 from persisten 不在IRPM_PCT CASE语句中。)

所以只需将您的上一个PMOD1_CD IN ('D')更改为PMOD1_CD IN ('C')