Если я понимаю вопрос, вы хотите развернуть данные и повторно агрегировать:
select v.x,
sum(case when week(date_time) = 27 then 1 else 0 end) as week_27,
sum(case when week(date_time) = 28 then 1 else 0 end) as week_28,
sum(case when week(date_time) = 29 then 1 else 0 end) as week_29,
sum(case when week(date_time) = 30 then 1 else 0 end) as week_30,
sum(case when week(date_time) = 31 then 1 else 0 end) as week_31
from fee f left join lateral
(values (f.x1), (f.x2), (f.x3), (f.x4)) v(x)
group by v.x
Presto также поддерживает filter
, поэтому его лучше записать как:
select v.x,
count(*) filter (where week(date_time) = 27) as week_27,
count(*) filter (where week(date_time) = 28) as week_28,
count(*) filter (where week(date_time) = 29) as week_29,
count(*) filter (where week(date_time) = 30) as week_30,
count(*) filter (where week(date_time) = 31) as week_31
from fee f left join lateral
(values (f.x1), (f.x2), (f.x3), (f.x4)) v(x)
group by v.x