我想生成一个计数表,显示每个范围内的分数在ms access数据库中出现的次数。
数据库表
smsId int, Age text,etc..
Age range | number of occurrences
-------------------------------------
0-9 | 11
10-19 | 14
20-29 | 3
... | ...什么是sql语法
发布于 2013-04-08 18:21:09
您可以使用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()添加更多的值。
另一种方法是添加一个表,其中包含报告的年龄范围。示例表可以是:
create table AgeRange
(
ageRangeStart number,
ageRangeEnd number
);
insert into AgeRange values
(0, 9),
(10, 19);如果创建此表,则可以将此表联接到现有表中,以报告范围:
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中测试的。
https://stackoverflow.com/questions/15876334
复制相似问题