问题描述
Kerrek所描述的扩展选项,您可以根据案例/时间进行分组
select
case when price >= 0 and price <= 10 then ' 0 - 10'
when price > 10 and price <= 50 then ' 10+ - 50'
when price > 50 and price <= 100 then ' 50+ - 100'
else 'over 100'
end PriceRange,
count(*) as TotalWithinRange
from
YourTable
group by 1
此处,“ group by 1”表示您的select语句中的序号列…在这种情况下,为case / when为TotalWithinRange时。
解决方法
我正在尝试编写一个查询,以返回价格落入特定时间段的商品数量的查询:
例如,如果我的表是:
item_name | price
i1 | 2
i2 | 12
i3 | 4
i4 | 16
i5 | 6
输出:
range | number of item
0 - 10 | 3
10 - 20 | 2
到目前为止,我这样做的方式是
SELECT count(*)
FROM my_table
Where price >=0
and price <10
然后
SELECT count(*)
FROM my_table
Where price >=10
and price <20
然后将每次粘贴的结果复制到excel中。
在sql查询中有自动的方法吗?