将所有结果合计为一

问题描述 投票:0回答:1

我有关于UNION ALL的查询,看起来像这样,但随后进行了相同的查询,但有其他条件

select count(id) filter (where data like '%"pmn":"CHILD_SEAT"%') as "Summary",
       count(id) filter (where data like '%"pmn":"CHILD_SEAT"%' and completed is true) as "Summary Completed",
       'Child Seat' "Type of car-class/seat"
from archived_order 
where created_user_login like 'exchangeAgent@shara' 
  and created between current_date - interval '1 day' and current_date

UNION ALL

select count(id) filter (where data like '%"pmi":2568%'),
       count(id) filter (where data like '%"pmi":2568%' and completed is true),
       'Child seat economy 1-3'
from archived_order 
where created_user_login not like 'exchangeAgent@shara' 
  and data like '%"cci":4%' 
  and created between current_date - interval '1 day' and current_date

UNION ALL

select count(id) filter (where data like '%"pmi":2568%') as a,
       count(id) filter (where data like '%"pmi":2568%' and completed is true),
       'Child seat standart 1-3'
from archived_order 
where created_user_login not like 'exchangeAgent@shara' 
  and data like '%"cci":1%' 
  and created between current_date - interval '1 day' and current_date;

我已经尝试通过执行as进行计数,并尝试使用count选择它,但是它似乎不起作用。我不确切地知道如何计算2列这样的所有条目count(id) filter (where data like '%"pmi":2568%')

以及所有此类条目count(id) filter (where data like '%"pmi":2568%' and completed is true)

我的目标是获得2个数字,该数字将是每个计数中所有值的总和。

postgresql
1个回答
0
投票

在三个条件下使用带有group by表达式的case

select count(id) as "Summary",
       count(id) filter (where completed) as "Summary Completed",
       case 
         when data like '%"pmn":"CHILD_SEAT"%' then 'Child Seat' 
         when data like '%"pmi":2568%' then 'Child seat economy 1-3'
         when data like '%"pmi":2568%' then 'Child seat standart 1-3'
       end "Type of car-class/seat" 
from archived_order 
where created_user_login like 'exchangeAgent@shara' and created between current_date - interval '1 day' and current_date
group by "Type of car-class/seat" 
© www.soinside.com 2019 - 2024. All rights reserved.