CASE WHEN in other columns [closed]

I have a data like this

YEAR Month Category ID Cost
2021 1 A 1 10
2021 1 A 2 5
2021 1 A 3 4
2021 1 B 4 3
2021 1 B 5 2
2021 1 C 6 12
2021 1 C 7 8

I tried to use CASE WHEN to sum categories to other columns but my code (bellow) returns NULL-s and work only for one category.

SELECT  
   YEAR(the_date)  
   ,month(the_date)  
  ,CASE WHEN Category IN (N'A','B') THEN SUM(Cost) END Category_1_Cost  
  ,CASE WHEN Category = 'C' THEN SUM(Cost) END Category_2_Cost  
  ,CASE WHEN Category = 'D' THEN SUM(Cost) END Category_3_Cost
 FROM 
  Base
GROUP 
  BY YEAR(the_date), month(the_date) ,  Category

Solution 1:

use this

SELECT  
   YEAR(the_date)  
   ,month(the_date)  
  ,CASE WHEN Category IN ('A','B') THEN SUM(Cost) ELSE 0 END Category_1_Cost  
  ,CASE WHEN Category = 'C' THEN SUM(Cost) ELSE 0 END Category_2_Cost  
  ,CASE WHEN Category = 'D' THEN SUM(Cost) ELSE 0 END Category_3_Cost
 FROM 
  Base
GROUP 
  BY YEAR(the_date), month(the_date) ,  Category

results:

(No column name) (No column name) Category_1_Cost Category_2_Cost Category_3_Cost
2021 1 19 0 0
2021 1 5 0 0
2021 1 0 20 0