简体   繁体   中英

Counting the number of records per month in an Access table which holds 25 years worth of data

This is probably a really simple question, but I couldn't find an answer which related to a dataset spread across such a long period of time. For starters, this carries on from an earlier post where I wanted to count the number of records from each month in a dataset which spanned several years - the body of the question was as follows:

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.

The comments on this question told me that it would be easier to import the csv file containing the data into Microsoft Access, and then query the data. I've followed the advice of those commenters and imported the (6,224,199) records into a new Access table, but now I'm stuck on writing the SQL query.

I don't have much experience with Access, but I gathered that I'm supposed to use a statement with a COUNT command to to add up the number of accident reports from each month. The problem with this is that the data spans 25 years, so with the (basic) statement which I cobbled together, I would have to run it 300 times in order to get the total number of reports for every month in every year.


EDIT

I've removed a lot of columns that were in the starting table - I did this because I only need to know how many accidents occurred per month, and the csv file/starting table had a lot of information that was of no use to me (eg road type, police force, light conditions). The table has the following columns in the following data types:

  • ID (AutoNumber)
  • Date (Date/Time)
  • Day_of_Week (Number)
  • Time (Date/Time)

In the output, all that I need is the number of accidents per month in the order of earliest (January 1979) to latest (December 2004). Because there will only be one figure for each month of each year, it would be possible to have a second column listing month/year, but this isn't necessary for me to use the data.

We could give you SQL, but I suspect that would be more confusing. So, assuming you're using the query UI:

You need to click the 'sum' symbol at the top (see image) to turn on aggregation, and try something like this:

访问查询用户界面窗口

Here's the SQL you can use to do this. It is essentially the same as what Ben has answered you with above. Just create a new query, and right-click and select SQL view. Then copy this in:

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");

Note: this SQL assumes your starting table is called Accidents . You will have to change this to your table name for both instances the table name Accidents is mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM