可以使用IIF()
表达式来创建组各年龄范围,再算上那些在每个区域中的行数:
select AgeRange, count(*) as NumberOfOccurrences
from
(
SELECT
iif(age >= 0 and age <=9, "0-9",
iif(age>=10 and age <=19, "10-19", "20-29")) as AgeRange
FROM yourtable
) d
group by AgeRange
如果您有更多的年龄范围,那么你将增加更多的价值到IIF()
。
另一种方法是添加一个包含报告年龄范围的表。样本表可以是:
如果你创建这个表,那么你可以将表与您现有的表的范围报告:
SELECT r.agerangestart &" - "&r.agerangeend as AgeRange,
count(*) as NumberOfOccurrences
from agerange r
inner join test t
on t.age >= r.agerangestart
and t.age <= r.agerangeend
group by r.agerangestart &" - "&r.agerangeend
这两个查询会给结果:
+----------+---------------------+
| AgeRange | NumberOfOccurrences |
+----------+---------------------+
| 0 - 9 | 6 |
| 10 - 19 | 4 |
| 20 - 29 | 2 |
+----------+---------------------+
注:这两个查询在MS Access测试2003
你expectin什么样的输出克从什么输入?使用更多详细信息编辑帖子可能会有所帮助。 – IndoKnight 2013-04-08 10:04:31
原始查询如下所示:select count(c_age)as no从短信的发生...其中...按age_range排序。什么是确切的sql? – decoder 2013-04-08 10:07:24
什么是c_age和短信?请说清楚你说的是什么。我的表格中没有看到c_age列。 – IndoKnight 2013-04-08 10:09:18