gpt4 book ai didi

sql - Access 中的组值范围

转载 作者:搜寻专家 更新时间:2023-10-30 23:45:25 24 4
gpt4 key购买 nike

我有一张表可以 Access 这些:

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())

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com