如何按年龄平均时间后创建子查询?我想把这个年龄组成一个'20 -29'组,并且该年龄组的总体平均值
SELECT [PAT Data].Gender, [PAT Data].Age, Avg([PAT Data].CourseTime) AS AvgOfCourseTime
FROM [PAT Data]
GROUP BY [PAT Data].Gender, [PAT Data].Age
HAVING ((([PAT Data].Gender)="F") AND (([PAT Data].Age)>="20" And ([PAT
Data].Age)<="29"))
UNION
SELECT [PAT Data].Gender, [PAT Data].Age, Avg([PAT Data].CourseTime) AS
AvgOfCourseTime
FROM [PAT Data]
GROUP BY [PAT Data].Gender, [PAT Data].Age
HAVING ((([PAT Data].Gender)="F") AND (([PAT Data].Age)>="30" And ([PAT
Data].Age)<="39"))
UNION
SELECT [PAT Data].Gender, [PAT Data].Age, Avg([PAT Data].CourseTime) AS
AvgOfCourseTime
FROM [PAT Data]
GROUP BY [PAT Data].Gender, [PAT Data].Age
HAVING ((([PAT Data].Gender)="F") AND (([PAT Data].Age)>="40" And ([PAT
Data].Age)<="49"));
答案 0 :(得分:0)
您可以使用switch()
:
SELECT SWITCH(Age >= 20 And Age <= 29, "20-29",
Age >= 30 And Age <= 39, "30-49",
Age >= 40 And Age <= 49, "40-59",
"Other") as agegrp,
Gender, Avg([PAT Data].CourseTime) AS AvgOfCourseTime
FROM [PAT Data]
WHERE [PAT Data].Gender) = "F"
GROUP BY [PAT Data].Gender,
SWITCH(Age >= 20 And Age <= 29, "20-29",
Age >= 30 And Age <= 39, "30-49",
Age >= 40 And Age <= 49, "40-59",
"Other") ;
答案 1 :(得分:0)
Interval
答案 2 :(得分:0)
您可以使用整数除法&#34; \&#34;和简单的连接:
SELECT
10 * (Age \ 10) & "-" & 10 * (Age \ 10) + 9 As AgeGroup,
Gender,
Avg(CourseTime) As AverageCourseTime
FROM
[PAT Data]
WHERE
Gender = "F"
GROUP BY
10 * (Age \ 10) & "-" & 10 * (Age \ 10) + 9
如果您希望达到最低年龄:
WHERE
Gender = "F",
Val(Age) >= 20
并将Age存储为整数,而不是文本。如果是数字,那么:
WHERE
Gender = "F",
Age >= 20