案例陈述计数

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

有人可以帮助我吗? 我在 BigQuery 上,我想要一个如下表;

结果 订单计数
215
379
90
with base_table as (
select order_id, o.customer_id, count( order_id) as count_of_order
from `orders_table` o
join `customers_table` c on o.customer_id = c.customer_id
group by 1,2
)
select 
case 
  when base_table.count_of_order < 500 then "low"
  when base_table.count_of_order between 501 and 1000 then "mid" 
  when base_table.count_of_order > 1000 then "high"
  else null 
  end as result,
count_of_order
from base_table
group by 1,2
count case
1个回答
0
投票

只需在

sum
上添加
count_of_order
即可?

with base_table as (
select order_id, o.customer_id, count( order_id) as count_of_order
from `orders_table` o
join `customers_table` c on o.customer_id = c.customer_id
group by 1,2
)
select 
case 
  when base_table.count_of_order < 500 then "low"
  when base_table.count_of_order between 501 and 1000 then "mid" 
  when base_table.count_of_order > 1000 then "high"
  else null 
  end as result,
sum(count_of_order) AS count_of_order
from base_table
group by 1
© www.soinside.com 2019 - 2024. All rights reserved.