473,378 Members | 1,434 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,378 software developers and data experts.

Query across Multiple Tables MAX()

I am new to sql and have not been able to resolve this issue. I have a query that shows me the information on students we serve. The query pulls all of their classes and the status results as well as contact information. I need to query down to the LAST class taken and the students status. If I take out the following fields I get the correct return but I have to have all the information dbo.Class.Year, dbo.ClassResult.ClassResultStatusID, dbo.Class.SemesterID, dbo.Location.LocationName, dbo.Institution.InstName

SELECT MAX(dbo.ClassResult.ClassID)AS LASTCLASS,dbo.Person.PersonID, dbo.Person.LastName, dbo.Person.FirstName,dbo.Address.Address1,dbo.Addr ess.City,dbo.Address.State, dbo.Address.Zip,dbo.Class.Year,dbo.ClassResult.Cla ssResultStatusID, dbo.Class.SemesterID,dbo.Location.LocationName, dbo.Institution.InstName

FROM dbo.Person INNER JOIN dbo.Person_Address ON dbo.Person.PersonID = dbo.Person_Address.PersonID INNER JOIN dbo.Address ON dbo.Person_Address.AddressID = dbo.Address.AddressID INNER JOIN dbo.ClassResult ON dbo.Person.PersonID = dbo.ClassResult.PersonID INNER JOIN dbo.Class ON dbo.ClassResult.ClassID = dbo.Class.ClassID INNER JOIN dbo.V_ClassResultStatusID ON dbo.ClassResult.ClassResultStatusID = dbo.ClassResult.ClassResultStatusID INNER JOIN dbo.V_SemesterID ON dbo.Class.SemesterID = dbo.Class.SemesterID INNER JOIN dbo.Location ON dbo.Class.LocationID = dbo.Location.LocationID INNER JOIN dbo.Institution ON dbo.Person.InstitutionID = dbo.Institution.InstitutionID

Group by dbo.Person.PersonID,dbo.Person.LastName,dbo.Person .FirstName,dbo.Address.Address1,dbo.Address.City, dbo.Address.State,dbo.Address.Zip,dbo.Class.Semest erID, dbo.Class.Year,dbo.ClassResult.ClassResultStatusID , dbo.Location.LocationName,dbo.Institution.InstName

Having (dbo.Class.Year= dbo.Class.Year and dbo.Class.SemesterID = Class.SemesterID)and count()> 1 and dbo.Location.LocationName = dbo.Location.LocationName and count()> 1 and dbo.Institution.InstName = dbo.Institution.InstName and count()> 1 and ((NOT (dbo.ClassResult.ClassResultStatusID = 8)) and count()> 1 OR (NOT (dbo.ClassResult.ClassResultStatusID = 10)) and count()> 1 OR (NOT (dbo.ClassResult.ClassResultStatusID = 13))and count()> 1 OR (NOT (dbo.ClassResult.ClassResultStatusID = 12)) and count()> 1OR (NOT (dbo.ClassResult.ClassResultStatusID = 4))and count()> 1)

ORDER BY dbo.Person.LastName, dbo.Person.FirstName
Nov 23 '09 #1
1 1382
Have cleaned it up
SELECT MAX(ClassResult.ClassID) AS LASTCLASS,
Person.PersonID,
Person.LastName,
Person.FirstName,
Address.Address1,
Address.City,
Address.State,
Address.Zip,
Class.Year,
ClassResult.ClassResultStatusID
FROM Person

INNER JOIN Person_Address ON Person.PersonID = Person_Address.PersonID
INNER JOIN Address ON Person_Address.AddressID = Address.AddressID
INNER JOIN ClassResult ON Person.PersonID = ClassResult.PersonID
INNER JOIN Class ON ClassResult.ClassID = Class.ClassID

GROUP BY Person.PersonID,
Person.LastName,
Person.FirstName,
Address.Address1,
Address.City,
Address.State,
Address.Zip,
Class.Year,
ClassResult.ClassResultStatusID
HAVING (ClassResult.ClassResultStatusID NOT IN (4, 8, 10, 12, 13))
ORDER BY Person.LastName, Person.FirstName
Nov 23 '09 #2

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

Similar topics

8
by: Rigga | last post by:
Hi, I am new to mysql and need help on how to join tables. I have a database which contains 4 tables, the main table contains information by date order and the other 3 contain data also in date...
14
by: Bob | last post by:
Hi there, Need a little help with a certain query that's causing a lot of acid in my stomach... Have a table that stores sales measures for a given client. The sales measures are stored per...
11
by: deko | last post by:
If I release a new version of my mbd (in mde format) and users want to upgrade - how do they migrate their data? For example, if the original was released as data1.mde and then I release...
3
by: Serious_Practitioner | last post by:
Good day, and thank you in advance for any assistance you can provide. I have a table in an Access 2000 .mdb file, and I've run into something odd and insolvable, at least for me. The database is...
0
by: schan | last post by:
Hi there, I was wondering if someone could shed some light on a problem I have no idea on how to fix. I created an Excel Add-In that uses an ADO connection to an Access database on a file...
4
by: Raj | last post by:
Hi all, I have couple of questions some one plzz help 1.I have a query which run for 2 hours on my production machine, it returns 1.5 millon rows, i looked at the explain plan it is picking up...
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
6
by: MVM | last post by:
Hi, I am attempting to run a query in MS SQL server between two tables that have a one to many relationship. The tables are linked on GID. What I want is one instance of every record from Table...
6
by: gerbski | last post by:
Hi all, I am relatively new to ADO, but up to now I got things working the way I wanted. But now I've run into somethng really annoying. I am working in MS Access. I am using an Access...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.