简体   繁体   中英

MS Access SQL statement count usage

I am new to SQL. I was given a coursework to report data of usage over the last 2 month. Can someone help me with the SQL statement?

SELECT COUNT(Member_ID,Non_Member_Name) AS Pool_usage_last_2_months
FROM Use_of_pool
WHERE DATEDIFF(‘2012-04-21’,’2012-02-21’) 

What I meant to do is to count the total number of member usage(member_ID) and non member usage(no ID,name only) from the last two months and then output the name and date and time,etc. on the same report. Is there any SQL statement to output that kind of information? Correction/Suggestions are welcomed.

You need a different WHERE clause. Assuming your Use_of_pool table includes a Date/Time field, date_field :

WHERE date_field >= #2012-02-21# AND date_field <= #2012-04-21#

If date_field values can include a time component other than midnight, advance the end date range by one day to capture all the possible Date/Time values from Apr. 21:

WHERE date_field >= #2012-02-21# AND date_field <= #2012-04-22#

That should restrict the rows to match what I think you want. It should offer fast performance with an index on date_field .

I'm unclear about the count(s) you want ... whether it is to be one count for all visits (both member and non-member), or separate counts for members and non-members.

Edit : If each row of the table represents a visit by one person, you can simply count the rows to determine the number of visits during your selected time frame.

SELECT Count(*) AS CountOfVisits
FROM Use_of_pool
WHERE date_field >= #2012-02-21# AND date_field <= #2012-04-21#

Notice each visit by the same person will contribute to CountOfVisits , which is what I think you want. If you wanted to know how many different people visited, we will need a different approach.

Edit2 : It sounds like you can use Member_ID and Non_Member_Name to distinguish between member and nonmember visits. Member_ID is Null for nonmembers and non-Null for members. And Non_Member_Name is Null for members and non-Null for nonmembers.

If that is true, try this query to count member and nonmember visits separately.

SELECT
    Sum(IIf(Member_ID Is Not Null, 1, 0)) AS member_visits,
    Sum(IIf(Non_Member_Name Is Not Null, 1, 0)) AS non_member_visits
FROM Use_of_pool
WHERE date_field >= #2012-02-21# AND date_field <= #2012-04-21#

Aggregate functions of SQL use all the data in a column (more precisely, all the data your WHERE clause selects) to produce a single datum. COUNT gives you the number of data rows that matched your WHERE clause. So for example:

SELECT COUNT(*) AS Non_members FROM Use_of_pool WHERE Member_ID IS NULL

will give you the number of times the pool was used by a non-member, and

SELECT COUNT(DISTINCT Member_ID) AS Members FROM Use_of_pool

will give you the number of members who have used the pool at least once (the DISTINCT tells the database engine to ignore duplicates when counting).

You can expand the WHERE clause to further specify what you want to count. If "last two months" means the current and previous calendar month, you'll need:

... WHERE DateDiff("m",Date_field,Date())<=1

If it means a rolling 2-month period, I'd approximate that with 60 days and say

... WHERE DateDiff("d",Date_field,Date())<60

(Replace Date_field with the name of the field containing the date.)

If you want to count rows according to multiple different criteria, or output both aggregate data and individual data, you'll be best off using separate SELECT statements.

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