需要您的SQL查询帮助,我应该如何编写它? 我有一个包含total_value和order_id的表,希望找到总值在0-50,51-100,超过100的订单数
谢谢!
答案 0 :(得分:5)
你想要一个案例陈述。
类似的东西:
select count(order_id),
value_range
from
(select order_id,
total_value,
case when total_value between 0 and 50 then '0-50'
when total_value between 51 and 100 then '51-100'
when total_value > 100 then '100'
end as value_range
from table)a
group by value_range
答案 1 :(得分:0)
也许这对你有用:
select count(order_id),
valueRange
from
(select order_id,
total_value,
case when total_value between 0 and 50 then '0-50'
when total_value between 51 and 100 then '51-100'
when total_value > 100 then '100'
end as valueRange
from table)a
Group by valueRange
答案 2 :(得分:0)
使用条件SUM:
SELECT SUM(CASE WHEN Total_Value BETWEEN 0 AND 50 THEN 1 ELSE 0 END) As Range1
, SUM(CASE WHEN Total_Value BETWEEN 51 AND 100 THEN 1 ELSE 0 END) As Range2
, SUM(CASE WHEN Total_Value > 100 THEN 1 ELSE 0 END) As Range3
FROM your_table