I have a table that has a primary key consisting of three fields:
Buildingid
Year
Subjectid
The other fields in this table record the number of teachers in the grade and students enrolled.
Example
Buildingid year subjectid countteachers enrollstudents
1 2008 12 5 100
1 2007 12 6 125
Here is my problem. I need to produce a report that shows the increase or decrease in countteachers and enrollstudents from one year to another. The structure of the report requested is as follows:
The report would be grouped by building
Staffing
Subject id 2007 teacher 2007 enrollment 2008 teachers 2008 enrollment +/- teachers +/- students
I can’t envision how to do this unless all this data is available in one record. I have thought about writing some code that would create a temp table that could loop through a recordset and and basically create a table they way I need it. Any ideas would be most appreciated.
5 2821
Is this what you mean -
SELECT a.BuildingID,a.Year,a.SubjectID,a.CountTeacher,
-
a.SubjectID-b.SubjectID as DeltaSubjects,
-
a.CountTeacher-b.CountTeacher as DeltaTeachers
-
FROM YourTable a
-
JOIN Youtable b on a.Year=b.year+1
-
you may need to swap things around in the subtractions in order to get the result you are after.
I hope it helps
Finally got this to work. Here is the answer: Thanks all! - SELECT Subjectid,
-
SUM(IIF(Year=2007,CountTeachers,0)) AS [2007 Teacher],
-
SUM(IIF(Year=2007,EnrollStudents,0)) AS [2007 Enrollment],
-
SUM(IIF(Year=2008,CountTeachers,0)) AS [2008 Teacher],
-
SUM(IIF(Year=2008,EnrollStudents,0)) AS [2008 Enrollment],
-
SUM(IIF(Year=2008,CountTeachers,0)-IIF(Year=2007,CountTeachers,0)) AS [+/- Teachers],
-
SUM(IIF(Year=2008,EnrollStudents,0)-IIF(Year=2007,EnrollStudents,0)) AS [+/- Students]
-
FROM yourtable
-
GROUP BY Subjectid
NeoPa 32,556
Expert Mod 16PB
I would consider something like the following. Just a little bit tidier. I'm guessing you probably have a different table name, but I'll use [YourTable] as in the other examples as I don't know it ;) I'm also assuming that [Year] is a numeric field rather than a text one, again from previous examples. - SELECT SubjectID,
-
Sum(IIf([Year]=2007,[CountTeachers],0)) AS [2007 Teachers],
-
Sum(IIf([Year]=2007,[EnrollStudents],0)) AS [2007 Enrollments],
-
Sum(IIf([Year]=2008,[CountTeachers],0)) AS [2008 Teachers],
-
Sum(IIf([Year]=2008,[EnrollStudents],0)) AS [2008 Enrollments],
-
([2008 Teachers]-[2007 Teachers]) AS [+/- Teachers],
-
([2008 Enrollments]-[2007 Enrollments]) AS [+/- Students]
-
FROM [YourTable]
-
GROUP BY [SubjectID]
THank you!! I like your tidy approach.
Patti
NeoPa 32,556
Expert Mod 16PB
My pleasure.
I did consider mentioning that the years should be flexible (to save you having to amend it every year) but the request was for titles including the year.
You should possibly consider this. Let us know if you do and want assistance.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Rajani |
last post by:
Hello,
I have a table(msaccess) with the structure...
job_code text 6
style text 10
qty number
fabrication text 65
ship_date date/time
|
by: Norma |
last post by:
I am trying to capture data in a date parameter query that spans from
1 to 3 months. I have a field that is a checkbox that notes whether or
not this data is 'red flagged' (so you know, the red...
|
by: Tony Williams |
last post by:
I have a table "tblmaintabs" that stores data that is collected from various
companies on a quarterly basis in March, June, September and December each
year (these dates are stored in a Date/time...
|
by: Rahul Chatterjee |
last post by:
Hello All
I have designed a dotnet application using VB which basically takes a
selection and passes value to a crystal report which in turn passes the
value to a Stored procedure. After the...
|
by: john |
last post by:
My report is based on a query with user input . In my report I
have a text box with the following code: =. This works when the
query outcome is 1 or more records. But when the query output is zero...
|
by: kabradley |
last post by:
Alrighty Guys and Gals, I have another question that I hope you all can help me with.
I have a report that uses a cross-tab query as its record source. This cross-tab query is getting all of its...
|
by: jmarcrum |
last post by:
I want to export a report (that contains two separate queries, 1. Current year data, and 2. split-year data) from access into excel, but everytime I run my code and export the data to excel, it looks...
|
by: n8kindt |
last post by:
i'm almost done with this project. one step still remains. here is the background:
we have a statement that needs to be emailed to a certain list of customers every month. however, that report...
|
by: OuTCasT |
last post by:
I know how to change the database and sqlserver for a crystal report
Dim report As New ReportDocument
Dim connection As IConnectionInfo
Dim oldServerName As String =...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |