我有一张这样的桌子:

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
...

谢谢你的回答!

最佳答案

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

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

aswitch应允许您按范围分组:
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

关于sql - 将 Access 值的范围分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29386443/

10-10 16:25