简体   繁体   中英

Using a Case Statement in a Function

I am having issues with a piece of a query I am writing- I'm creating an indicator field that I only want to show up as '1' if a couple of cases are satisfied. The field is built using a case statement inside a MAX() function- I need the function because this portion is part of a CTE that is joined with another CTE that is turning rows into columns and I used functions to lateralize.

 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

Basically I check one field to see if 'IR' is present and that its percent is not equal to 0- if so I want an indicator of 1. But when I run this, there are still some entries coming through with an indicator of 1 and a @IRPM_PCT of 0. Any advice? Does it have to do with the case statements being in a MAX() function? When I check the data that comes through, it seems to "work" but I would like to eliminate these indicator of 1, percent of 0 cases and I thought the way I built the Indicator case statement would deal with this.

In the IRPM_PCT CASE statement, you check PMOD6_PGM for 'C' twice (instead of for 'C' and then for 'D').

So if PMOD6_PGM = 'IR', PMOD6_PCT is non-zero, and PMOD1_CD = 'D', then you'll get IRPM_IND = 1 and IRPM_PCT = 0 (because PMOD1_CD IN ('D') is not in the IRPM_PCT CASE statement).

So just change your last PMOD1_CD IN ('C') to PMOD1_CD IN ('D') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM