Solved

MS Access - Altering query to make totals by category

Posted on 2014-01-03
3
177 Views
Last Modified: 2014-01-03
The code below produces a summary of enrollment by school for a given year.  I would like to alter this query so that I have totals by school type as opposed to individual schools.

For example, instead of listing the total for each elementary school, I want one total for school type 1, school type 2 and school type 3.  Thanks for your insight in advance.

SELECT enrollment.er_school_id, Sum(IIf([Year]=forms!select_dates!current_year,[enroll_data],0)) AS [Current Enrollment], Sum(IIf([Year]=forms!select_dates!proposed_year,[enroll_data],0)) AS [Proposed Enrollment], ([Proposed Enrollment]-[Current Enrollment]) AS [+/- Enrollment], schools.schoollName, schools.school_id, schools.Type
FROM schools INNER JOIN enrollment ON schools.school_id = enrollment.er_school_id
WHERE (((schools.open_date)<="forms]![select_dates]![current_year]") AND ((schools.close_date)>=[forms]![select_dates]![proposed_year]))
GROUP BY enrollment.er_school_id, schools.schoollName, schools.school_id, schools.Type;

Open in new window

0
Comment
Question by:Sbovino
3 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 39753745
SELECT enrollment.er_school_id, Sum(IIf([Year]=forms!select_dates!current_year,[enroll_data],0)) AS [Current Enrollment], Sum(IIf([Year]=forms!select_dates!proposed_year,[enroll_data],0)) AS [Proposed Enrollment], ([Proposed Enrollment]-[Current Enrollment]) AS [+/- Enrollment], schools.schoollName, schools.school_id, schools.Type
FROM schools INNER JOIN enrollment ON schools.school_id = enrollment.er_school_id
WHERE (((schools.open_date)<="forms]![select_dates]![current_year]") AND ((schools.close_date)>=[forms]![select_dates]![proposed_year]))
GROUP BY schools.Type;
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39753773
Are your [Schools].[Open_Date] and [Schools].[Close_Date] fields actually dates?  If so, your Where clause seems flawed, as well as the fact that there should be no quotes in your where clause

SELECT Schools.Type
, Sum(IIf([Year]=forms!select_dates!current_year,[enroll_data],0)) AS [Current Enrollment]
, Sum(IIf([Year]=forms!select_dates!proposed_year,[enroll_data],0)) AS [Proposed Enrollment]
, ([Proposed Enrollment]-[Current Enrollment]) AS [+/- Enrollment]
FROM schools
INNER JOIN enrollment
ON schools.school_id = enrollment.er_school_id
WHERE (((schools.open_date)<=[forms]![select_dates]![current_year])
AND ((schools.close_date)>=[forms]![select_dates]![proposed_year]))
GROUP BY  schools.Type
0
 

Author Closing Comment

by:Sbovino
ID: 39753918
Worked perfectly.  Thank you for the comments -  they were very helpful.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Query 18 82
Access Database - Manually changing xid value 7 45
SQL syntax error in VBA 11 33
SQL Select Query help 3 32
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now