【问题标题】:Group by Range ms access database按范围分组 ms 访问数据库
【发布时间】:2013-04-08 10:02:00
【问题描述】:

我想生成一个计数表,显示在 ms 访问数据库的每个范围中出现了多少次分数。

数据库表

smsId int, Age text,etc.. 

Age   range  | number of occurrences
-------------------------------------
   0-9       |        11
  10-19      |        14
  20-29      |         3
   ...       |       ...

什么是sql语法

【问题讨论】:

  • 您期望从什么输入得到什么样的输出?使用更多详细信息编辑您的帖子可能会有所帮助。
  • 原始查询如下:select count(c_age) as no of occurrences from sms where... group by age_range.what is exact sql ?
  • 这里的c_age和sms是什么?请把你说的清楚。我在您的表中没有看到 c_age 列。
  • 它是年龄 field.count(age),因为没有出现

标签: sql ms-access group-by range


【解决方案1】:

您可以使用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 中进行了测试。

【讨论】:

    猜你喜欢
    • 2013-04-27
    • 2013-06-19
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多