简体   繁体   中英

Applying ISNULL inside of a CASE expression

I need to have a way to apply ISNULL in a CASE expression that will replace the NULL values with 0. The below code runs but still returns NULL values. Is there any way to accomplish this? There are two stock types - 'A' and blank, I am trying to get the sum of the quantity for each type.

I've tried using ISNULL inside the CASE

CASE WHEN MRP.stock_type = 'A' 
    THEN ISNULL(SUM(MRP.QUANTITY),0)   
END AS 'Uncovered_Quantity', 
CASE WHEN MRP.stock_type = ' '
    THEN  ISNULL(SUM(MRP.QUANTITY),0)
END AS 'Blank_Quantity',

I think you probably intended conditional aggregation:

SUM(CASE WHEN MRP.stock_type = 'A' THEN MRP.QUANTITY ELSE 0 END) as Uncovered_Quantity, 
SUM(CASE WHEN MRP.stock_type = ' ' THEN MRP.QUANTITY ELSE 0 END) as Blank_Quantity,

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