Lauren
Lauren

Reputation: 65

Null values affecting case logic

enter image description here

SELECT
    station,

    CASE 
        WHEN ar IS NULL THEN '0'
        ELSE ar
    END AS ar,

    del_date,
    sum( volume ) / 7 AS volume_ref,
    
    DATE_PART("week", del_date) AS week_num,
    DATE_PART("year", del_date) AS year,
    
    CASE 
        WHEN volume_ref between     0 AND 20000 AND ar <> 'YES' THEN 'ds x-small'
        WHEN volume_ref between 20000 AND 36000 AND ar <> 'YES' THEN 'ds small'
        WHEN volume_ref between 36000 AND 42000 AND ar <> 'YES' THEN 'ds standard'
        WHEN volume_ref between 42000 AND 72000 AND ar <> 'YES' THEN 'ds large'
        WHEN volume_ref                 > 72000                 THEN 'ds x-large'
        WHEN                                        ar =  'YES' THEN 'ds x-large'
        ELSE                                                         'ds small'
    END AS station_ref

FROM
    prophecy_na.na_topology_lrp
    LEFT JOIN wbr_global.raw_station_extended_attribute ON 
        prophecy_na.na_topology_lrp.station   = 
        wbr_global.raw_station_extended_attribute.ds 
WHERE
    week_num IN ( 16, 20, 40, 48 )
GROUP BY
    na_topology_lrp.station,
    raw_station_extended_attribute.ar,
    na_topology_lrp.del_date;

raw_station_extended_attribute table enter image description here

na_topology_lrp table enter image description here

Upvotes: 1

Views: 86

Answers (2)

Lauren
Lauren

Reputation: 65

SELECT station,
isnull(ar,'0') as dsar,
del_date,sum(volume) /7 as volume_ref,DATE_PART("week",del_date) as week_num,DATE_PART("year",del_date) as year,
CASE 
when volume_ref between 0 and 20000 and dsar <> 'YES' then 'ds x-small'
when volume_ref between 20000 and 36000 and dsar <> 'YES' then 'ds small'
when volume_ref between 36000 and 42000 and dsar <> 'YES' then 'ds standard'
when volume_ref between 42000 and 72000 and dsar <> 'YES' then 'ds large'
when volume_ref > 72000 then 'ds x-large'
when dsar = 'YES' then 'ds x-large'
else 'ds small' end as station_ref
FROM prophecy_na.na_topology_lrp
Left JOIN wbr_global.raw_station_extended_attribute ON prophecy_na.na_topology_lrp.station = wbr_global.raw_station_extended_attribute.ds 
where week_num in(16,20,40,48)
Group by na_topology_lrp.station,raw_station_extended_attribute.ar,na_topology_lrp.del_date;

Upvotes: 1

Tyler
Tyler

Reputation: 38

I believe that the highlighted row has a station_ref of 'ds small' because of the ELSE statement at the end of the 2nd case statement. Further, you are using multiple 'ar' references here - 'ar' is used to reference the column both before and after processing the 1st CASE statement.

Try this:

SELECT
    station,

    CASE 
        WHEN ar IS NULL THEN '0'
        ELSE ar
    END AS ar,

    del_date,
    sum( volume ) / 7 AS volume_ref,
    
    DATE_PART("week", del_date) AS week_num,
    DATE_PART("year", del_date) AS year,
    
    CASE 
        WHEN volume_ref between     0 AND 20000 AND REPLACE(ar,NULL,'0') <> 'YES' THEN 'ds x-small'
        WHEN volume_ref between 20000 AND 36000 AND REPLACE(ar,NULL,'0') <> 'YES' THEN 'ds small'
        WHEN volume_ref between 36000 AND 42000 AND REPLACE(ar,NULL,'0') <> 'YES' THEN 'ds standard'
        WHEN volume_ref between 42000 AND 72000 AND REPLACE(ar,NULL,'0') <> 'YES' THEN 'ds large'
        WHEN volume_ref                 > 72000                 THEN 'ds x-large'
        WHEN                                        REPLACE(ar,NULL,'0') =  'YES' THEN 'ds x-large'
        ELSE                                                         'ds small'
    END AS station_ref

FROM
    prophecy_na.na_topology_lrp
    LEFT JOIN wbr_global.raw_station_extended_attribute ON 
        prophecy_na.na_topology_lrp.station   = 
        wbr_global.raw_station_extended_attribute.ds 
WHERE
    week_num IN ( 16, 20, 40, 48 )
GROUP BY
    na_topology_lrp.station,
    raw_station_extended_attribute.ar,
    na_topology_lrp.del_date;

Upvotes: 1

Related Questions