访问中的组值范围

Group ranges of values in access

我有一个 table 可以访问这些:

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

还有很多。

我如何进行 sql 查询来获取 table 中人们的年龄、计算它并显示范围?

类似于:

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

感谢您的回答!

您可以使用 datediff:

计算某人的年龄
datediff('yyyy', Birthdate, now())

A 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

可能对你有帮助

    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