【问题标题】:Counting the number of records per month in an Access table which holds 25 years worth of data计算包含 25 年数据的 Access 表中每月的记录数
【发布时间】:2015-02-10 10:03:55
【问题描述】:

这可能是一个非常简单的问题,但我找不到与分布在如此长一段时间内的数据集相关的答案。 对于初学者来说,这是从之前的一篇文章继续进行的,我想计算跨越数年的数据集中每个月的记录数 - 问题的主体如下:

I've downloaded a dataset which details all of the car accidents reported in England between January 1979 and December 2004 - this file is in csv format and is understandably quite large (6,224,199 rows, to be exact). Because the size of the file exceeds the number of rows that Excel 2010 can handle, I'd have to split the file into smaller ones in order to open it all at once in Excel. I tried using Notepad and Notepad++, but Notepad crashed, and Notepad++ refused to open such a large (720MB) file. I've considered using an Excel replacement like Delimit, but it doesn't support Macros. Now, overlooking the size issue, I need to count the total number of crashes from each month and make a note of them. There's a column to specify the date of each crash, but the rows aren't sorted according to the crash date. I was considering using CTRL+F to count the number of rows with a specific month/year value and then logging the number of results for each search, but considering that the data spans 25 years, I'd have to manually search and record the results from 300 months.

这个问题的cmet告诉我,将包含数据的csv文件导入Microsoft Access,然后查询数据会更容易。我听从了这些评论者的建议,将 (6,224,199) 条记录导入到一个新的 Access 表中,但现在我一直在编写 SQL 查询。

我没有太多使用 Access 的经验,但我了解到我应该使用带有 COUNT 命令的语句来将每个月的事故报告数量相加。这样做的问题是数据跨越 25 年,因此使用我拼凑的(基本)语句,我必须运行 300 次才能获得每年每个月的报告总数。


编辑

我已经删除了起始表中的很多列 - 我这样做是因为我只需要知道每月发生了多少事故,并且 csv 文件/起始表有很多没有的信息习惯于我(例如道路类型、警察部队、光照条件)。该表具有以下数据类型的以下列:

  • ID(自动编号)
  • 日期(日期/时间)
  • Day_of_Week(数字)
  • 时间(日期/时间)

在输出中,我只需要按从最早(1979 年 1 月)到最晚(2004 年 12 月)的顺序排列每月的事故数量。因为每年的每个月只有一个数字,所以可以有第二列列出月/年,但这不是我使用数据的必要条件。

【问题讨论】:

  • 为了让我们为您提供适当的帮助,您必须告诉我们您的起始表格有哪些列,以及您希望最终表格有哪些列
  • group by子句

标签: sql excel ms-access csv dataset


【解决方案1】:

我们可以为您提供 SQL,但我怀疑这会更令人困惑。 因此,假设您使用的是查询 UI:

您需要单击顶部的“求和”符号(见图)以打开聚合,然后尝试以下操作:

【讨论】:

    【解决方案2】:

    这是您可以用来执行此操作的 SQL。这与本在上面回答您的内容基本相同。只需创建一个新查询,然后右键单击并选择 SQL 视图。然后将其复制到:

    SELECT Format([Date],"yyyy-mm") AS [Month of Crashes], Count([Accidents].ID) AS [Crashes this Month] INTO Crashes_per_Month
    FROM [Accidents]
    GROUP BY Format([Date],"yyyy-mm");
    

    注意:此 SQL 假定您的起始表名为 Accidents。对于提到表名Accidents 的两个实例,您必须将其更改为您的表名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多