如何使用BETWEEN,COUNT和ALIAS显示数字范围

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

如何使用BETWEEN,COUNT和ALIAS显示同一列中数字范围的数字范围。

比如10-19,20-29,e.t.c

sql
2个回答
0
投票
select 
  * 
from table 
where number between 10 and 99

PS:这里的表是表名,而数字是列名。

计数:它将显示数据库中的记录总数。

select count(names) from table;

ALIAS:Alias用于重命名列或表


0
投票

使用GROUP BY round(grouping-field / 10,0)。你正在做的是使用整数除法来创建分组字段的组。不需要使用BETWEEN。也不是ALIAS

我使用SQLite对我的一些数据进行了快速测试:

select round((pPrice-5) / 10,0)*10 as grping_bottom, 
  round((pPrice-5) / 10 +1,0)*10 as grping_top_is_below, 
  count(pPrice), sum(pPrice), min(pPrice), max(pPrice)
  from oprice
  group by round((pPrice-5) / 10,0)

我用十个作为我的团队规模。您的需求可能不同。

但是,你真的想使用FLOOR()CEILING()INT(),而不是ROUND(),因为5到14之间的值将四舍五入到10-20组。由于我在SQLite中没有这些功能,因此从分组字段中减去5会为10的分组创建适当的偏移量。

未经测试 - 如果有FLOOR()功能,那么...

select floor(pPrice / 10)*10 as grping_bottom, 
  floor(pPrice / 10 +1)*10 as grping_top_is_below, 
  count(pPrice), sum(pPrice), min(pPrice), max(pPrice)
  from oprice
  group by floor(pPrice / 10)

快乐的编码!

© www.soinside.com 2019 - 2024. All rights reserved.