How to avoid redundant case/when in Postgres? Based on this simple example table:

create table some_data (x integer, y integer);

insert into some_data (x,y) values
(1,1),
(1,2),
(1,1000),
(1,2),
(2,1),
(2,1),
(2,2);


I query it trying to sum the values based on particular conditions, like:

select
  case when x = 1 then
        sum(case when y = 1 then 1 else 0 end) as counter_x,
        sum(case when y = 2 then 2 else 0 end) as counter_y,
        sum(case when y &gt; 2 then 7 else 0 end) as counter_z,
      end,

  sum(case when x = 1 then 1 else 0 end) as counter_a,
  sum(case when x = 2 and y = 1 then 10 else 0 end) as counter_b,
  sum(case when x = 3 and y = 30 then 400 else 0 end) as counter_c
  from some_data;


What I am expecting here is:

counter_x | counter_y | counter_z | counter_a | counter_b | counter_c
----------+-----------+-----------+-----------+-----------+----------
1         | 4         | 7         | 4         | 20        | 0


However my query doesn't work as the wrapping case when x = 1 is not allowed.
How can I fix it?
You should add a condition for every counter:

select
    sum(case when x = 1 and y = 1 then 1 else 0 end) as counter_x,
    sum(case when x = 1 and y = 2 then 2 else 0 end) as counter_y,
    sum(case when x = 1 and y &gt; 2 then 7 else 0 end) as counter_z,
    sum(case when x = 1 then 1 else 0 end) as counter_a,
    sum(case when x = 2 and y = 1 then 10 else 0 end) as counter_b,
    sum(case when x = 3 and y = 30 then 400 else 0 end) as counter_c
from 
    some_data;

counter_x | counter_y | counter_z | counter_a | counter_b | counter_c
--------: | --------: | --------: | --------: | --------: | --------:
        1 |         4 |         7 |         4 |        20 |         0


dbfiddle here