?
Solved

MS Access - Altering query to make totals by category

Posted on 2014-01-03
3
Medium Priority
?
188 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 49

Accepted Solution

by:
Dale Fye earned 2000 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 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