Solved

MS Access - Altering query to make totals by category

Posted on 2014-01-03
3
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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