Access 2010 中的分组年龄范围

Posted

技术标签:

【中文标题】Access 2010 中的分组年龄范围【英文标题】:Grouping age range in Access 2010 【发布时间】:2014-03-13 15:20:51 【问题描述】:

我有一个带有 ClientID (PK) 的 Client 表和带有 Date/Time 作为数据类型的 DOB 字段。我需要创建一个查询来锻炼年龄组,例如:

Age Group  Count

18 – 24      2
25 - 35      1
36 - 45      2
46 - 55      5
56 - 65      0
66+          2
Not stated   5
Total        17

我有以下 sql 查询:

SELECT DISTINCT Partition(DateDiff("yyyy",[DOB],Date())(DateSerial(Year(Date()),Month([DOB]),Day([DOB]))>Date()),19,66,8) AS Range, 
Count(ClientsDetails.ClientID) AS CountOfAgeGroups
FROM ClientsDetails
GROUP BY Partition(DateDiff("yyyy",[DOB],Date())+(DateSerial(Year(Date()),Month([DOB]),Day([DOB]))>Date()),19,66,8);

它会产生以下结果:

Range    CountOfAgeGroup   
:18      1
27:34    4
35:42    1
67:      1    

由于我对 Access 不太熟悉,请有人指导我应该对查询进行哪些更改,或者可能建议最佳解决方案以实现预期结果。

提前致谢。

这是我查询的第二部分。是否也可以使用参数搜索找到两个日期之间的年龄组。例如,如果我想查找 01/01/14 和 17/03/14 之间的年龄组。我使用 Gord Thompson 发布的解决方案尝试了以下操作,但它抛出了不正确的表达式错误:

SELECT [%$##@_Alias].AgeGroup, Count(*) AS CountOfAgeGroup, Support.ClientID
FROM (SELECT Switch([Age] Between 18 And 24, "18-24", [Age] Between 25 And 35, "25-35", [Age] Between 36 And 45, "36-45", [Age] Between 46 And 55, "46-55",[Age] Between 56 And 65,  "56-65",[Age] > 66, "66+") AS AgeGroup FROM (SELECT DateDiff("yyyy",[DOB],Date())-IIf(Format(Date(),"mmdd")<Format([DOB],"mmdd"),1,0) AS Age FROM ClientsDetails)  AS [%$##@_Alias])  AS [%$##@_Alias], Support
GROUP BY [%$##@_Alias].AgeGroup,Support.ClientID
HAVING (((Support.ClientID) Between [Enter Start Date] And [Enter End Date]));

【问题讨论】:

现在可以使用以下方法解决此问题: SELECT AgeGroup, Count(*) As CountOfAgeGroup FROM ( SELECT SWITCH ([Age] 介于 18 和 24 之间, "18-24", [Age] 介于 25 And 35, "25-35", [Age] 36-45, "36-45", [Age] 46-55, "46-55",[Age] 56-65, "56-65" ,[Age] > 66, "66+") AS AgeGroup FROM ( SELECT DateDiff("yyyy",[DOB],Date())-IIf(Format(Date(),"mmdd") 【参考方案1】:

如果我们从一个计算实际年龄的查询开始(基于 [Date_of_Birth] 字段和Date()函数)

SELECT 
    DateDiff("yyyy",[Date_of_Birth],Date())-IIf(Format(Date(),"mmdd")<Format([Date_of_Birth],"mmdd"),1,0) AS Age
FROM Students

然后我们可以将其用作子查询并应用 Switch() 函数(参考:here)为每个条目分配一个 [AgeGroup]

SELECT
    Switch([Age] Between 0 And 17, "0-17", [Age] Between 18 And 24, "18-24", [Age] Between 25 And 35, "25-35", [Age] > 35, "Over 35") AS AgeGroup
FROM
    (
        SELECT 
            DateDiff("yyyy",[Date_of_Birth],Date())-IIf(Format(Date(),"mmdd")<Format([Date_of_Birth],"mmdd"),1,0) AS Age
        FROM Students
    )

然后将整个事情包装在一个聚合查询中

SELECT
    AgeGroup,
    Count(*) AS CountOfAgeGroup
FROM
    (
        SELECT
            Switch([Age] Between 0 And 17, "0-17", [Age] Between 18 And 24, "18-24", [Age] Between 25 And 35, "25-35", [Age] > 35, "Over 35") AS AgeGroup
        FROM
            (
                SELECT 
                    DateDiff("yyyy",[Date_of_Birth],Date())-IIf(Format(Date(),"mmdd")<Format([Date_of_Birth],"mmdd"),1,0) AS Age
                FROM Students
            )
    )
GROUP BY AgeGroup

得到这样的结果

AgeGroup  CountOfAgeGroup
--------  ---------------
0-17                    5
18-24               12897
25-35                3537
Over 35               766

【讨论】:

以上是关于Access 2010 中的分组年龄范围的主要内容,如果未能解决你的问题,请参考以下文章

分组访问中的值范围

按年龄分组和计数

在access报表中,按"编号"字段前四位分组统计每组记录的平均年龄,并将统计结果显示在组页脚节区.怎么做?

Apache Spark SQL 按范围分组数据

Oracle:如何在一个范围内“分组”?

使用 Access 2010 数据库的 Excel 2010 VBA DAO 查询不起作用