分组访问中的值范围
Posted
技术标签:
【中文标题】分组访问中的值范围【英文标题】: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
...
感谢您的回答!
【问题讨论】:
你是如何组成这个小组的?这些是静态值吗? 【参考方案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
【讨论】:
太棒了!更多关于开关功能:techonthenet.com/access/functions/advanced/switch.php【参考方案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
(我的版本不支持)以上是关于分组访问中的值范围的主要内容,如果未能解决你的问题,请参考以下文章
mapReduce/Aggregation:按嵌套文档中的值分组