【问题标题】:Group ranges of values in access分组访问中的值范围
【发布时间】:2015-04-01 08:46:25
【问题描述】:

我有一张可以访问的表,如下所示:

Name:-----Birthdate:-----Section----etc...
John------10/10/1985-----etc...  
Mike------02/03/1976-----etc...  

还有更多。

如何进行 sql 查询来获取表中人员的年龄、计数并显示范围?

类似:

Group1 ( From 18 to 25 ): 2 people  
Group2 ( From 26 to 35 ): 1 person  
...

感谢您的回答!

【问题讨论】:

  • 你是如何组成这个小组的?这些是静态值吗?

标签: sql database ms-access group-by


【解决方案1】:

您可以使用datediff 计算某人的年龄:

datediff('yyyy', Birthdate, now())

switch 应该允许您按范围分组:

select  AgeGroup            
,       count(*)
from    (
        select  switch(
                  datediff('yyyy', Birthdate, now()) between 18 and 25, '18 to 25',
                  datediff('yyyy', Birthdate, now()) between 26 and 35,  '26 to 35',
                  true, 'other') as AgeGroup 
        from    YourTable
        ) as SubQueriesMustBeNamed
group by 
        AgeGroup

【讨论】:

【解决方案2】:

也许对你有帮助

    select d,cast(count(d) as nvarchar(max)) + ' persons' as total  from
(
    select case 
           when CONVERT(int,ROUND(DATEDIFF(hour,Birthdate,GETDATE())/8766.0,0)) between 10 and 20   then '10-20' 
           else '>20' end as d from  YourTable
) a
    group by d

【讨论】:

  • 不确定 ms-access 是否支持case(我的版本不支持)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
相关资源
最近更新 更多