473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Report - How to compare year to year data

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.
Mar 7 '08 #1
5 2821
Delerna
1,134 Expert 1GB
Is this what you mean
Expand|Select|Wrap|Line Numbers
  1. SELECT a.BuildingID,a.Year,a.SubjectID,a.CountTeacher,
  2.         a.SubjectID-b.SubjectID as DeltaSubjects,
  3.         a.CountTeacher-b.CountTeacher as DeltaTeachers
  4. FROM YourTable a
  5. JOIN  Youtable b on a.Year=b.year+1
  6.  
you may need to swap things around in the subtractions in order to get the result you are after.
I hope it helps
Mar 7 '08 #2
Finally got this to work. Here is the answer: Thanks all!



Expand|Select|Wrap|Line Numbers
  1. SELECT Subjectid,
  2.        SUM(IIF(Year=2007,CountTeachers,0)) AS [2007 Teacher],
  3.        SUM(IIF(Year=2007,EnrollStudents,0)) AS [2007 Enrollment],
  4.        SUM(IIF(Year=2008,CountTeachers,0)) AS [2008 Teacher],
  5.        SUM(IIF(Year=2008,EnrollStudents,0)) AS [2008 Enrollment],
  6.        SUM(IIF(Year=2008,CountTeachers,0)-IIF(Year=2007,CountTeachers,0)) AS [+/- Teachers],
  7.        SUM(IIF(Year=2008,EnrollStudents,0)-IIF(Year=2007,EnrollStudents,0)) AS [+/- Students]
  8. FROM yourtable
  9. GROUP BY Subjectid
Mar 8 '08 #3
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.
Expand|Select|Wrap|Line Numbers
  1. SELECT SubjectID,
  2.        Sum(IIf([Year]=2007,[CountTeachers],0)) AS [2007 Teachers],
  3.        Sum(IIf([Year]=2007,[EnrollStudents],0)) AS [2007 Enrollments],
  4.        Sum(IIf([Year]=2008,[CountTeachers],0)) AS [2008 Teachers],
  5.        Sum(IIf([Year]=2008,[EnrollStudents],0)) AS [2008 Enrollments],
  6.        ([2008 Teachers]-[2007 Teachers]) AS [+/- Teachers],
  7.        ([2008 Enrollments]-[2007 Enrollments]) AS [+/- Students]
  8. FROM [YourTable]
  9. GROUP BY [SubjectID]
Mar 9 '08 #4
THank you!! I like your tidy approach.

Patti
Mar 9 '08 #5
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.
Mar 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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
3
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...
5
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...
0
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...
2
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...
69
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...
3
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...
6
n8kindt
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...
7
OuTCasT
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 =...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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
0
BarryA
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...
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.