Integrate query in query...

zozew

Registered User.
Local time
Today, 19:35
Joined
Nov 18, 2010
Messages
199
Hi Guys,

The Query below finds the most recent record in a table according to a date field, my problem is i don't know how to integrate this Query into the Query below this Query to replace the "Last" function

Code:
SELECT a.inmateID,
       a.classificationID,
       b.max_date
FROM (
    SELECT tblClassificationHistory.inmateID, 
           tblClassificationHistory.classificationID,                
           tblClassificationHistory.reclassificationDate
    FROM tblinmate
    INNER JOIN tblClassificationHistory
    ON tblinmate.inmateID = tblClassificationHistory.inmateID 
) a
INNER JOIN (
    SELECT tblClassificationHistory.inmateID, 
           MAX(tblClassificationHistory.reclassificationDate) as max_date
    FROM tblinmate
    INNER JOIN tblClassificationHistory
    ON tblinmate.inmateID = tblClassificationHistory.inmateID
    GROUP BY tblClassificationHistory.inmateID  
) b
ON a.inmateID = b.inmateID
AND a.reclassificationDate = b.max_date
ORDER BY a.inmateID;

The "Last" function in the query below (line 4 & 5) is not exactly what I'm after. The "Last" function finds the last record in that table, but i need to find the most recent classificationid in the table according to reclassificationDate field. (same for squadID)

Code:
SELECT
        tblinmate.statusid,
        tblinmate.activedate,
        Last(tblclassificationhistory.classificationid) AS LastOfclassificationID,
        Last(tblsquadhistory.squadid) AS LastOfsquadID,
        tblperson.firstname,
        tblperson.middlename,
        tblperson.lastname,
        tblinmate.prisonnumber,
        tblinmate.droppeddate,
        tblinmate.personid,
        tblinmate.inmateid
FROM tblsquad
INNER JOIN (tblperson
INNER JOIN ((tblinmate
INNER JOIN (tblclassification
INNER JOIN tblclassificationhistory
        ON tblclassification.classificationid =
        tblclassificationhistory.classificationid)
        ON tblinmate.inmateid =
        tblclassificationhistory.inmateid)
INNER JOIN tblsquadhistory
        ON tblinmate.inmateid =
        tblsquadhistory.inmateid)
        ON tblperson.personid = tblinmate.personid)
        ON tblsquad.squadid = tblsquadhistory.squadid
GROUP BY tblinmate.statusid,
         tblinmate.activedate,
         tblperson.firstname,
         tblperson.middlename,
         tblperson.lastname,
         tblinmate.prisonnumber,
         tblinmate.droppeddate,
         tblinmate.personid,
         tblinmate.inmateid;
 
Last edited:
I have managed to integrate the max_date function for one field classificationID into the query but how do i repeat the exact process for the squadID according to the most recent changeSquadDate? just as i have done for the classificationID...im doing all this by testing one line at a time...

Code:
SELECT a.inmateID,
       a.squadID,
       b.max_date
FROM (
    SELECT tblSquadHistory.inmateID, 
           tblSquadHistory.squadID,                
           tblSquadHistory.squadChangeDate
    FROM tblinmate
    INNER JOIN tblSquadHistory
    ON tblinmate.inmateID = tblSquadHistory.inmateID 
) a
INNER JOIN (
    SELECT tblSquadHistory.inmateID, 
           MAX(tblSquadHistory.squadChangeDate) as max_date
    FROM tblinmate
    INNER JOIN tblSquadHistory
    ON tblinmate.inmateID = tblSquadHistory.inmateID
    GROUP BY tblSquadHistory.inmateID  
) b
ON a.inmateID = b.inmateID
AND a.squadChangeDate = b.max_date
ORDER BY a.inmateID;
into this...

Code:
SELECT
        a.inmateID,
        a.personID,
        a.classificationID,
        a.statusID,
        a.prisonNumber,
        a.activeDate,
        a.firstName,
        a.middleName,
        a.lastName,
        a.photo,
        b.max_date
FROM (
        SELECT
        tblPerson.personID,
        tblPerson.firstName,
        tblPerson.photo,
        tblPerson.middleName,
        tblPerson.lastName,
        tblinmate.statusID,
        tblinmate.prisonNumber,
        tblinmate.activeDate,
        tblClassificationHistory.inmateID,
        tblClassificationHistory.classificationID,
        tblClassificationHistory.reclassificationDate
        FROM tblPerson
        INNER JOIN (tblinmate
        INNER JOIN tblClassificationHistory
        ON tblinmate.inmateID = tblClassificationHistory.inmateID)
        ON tblPerson.personID = tblinmate.personID
) AS a
INNER JOIN (
        SELECT
        tblClassificationHistory.inmateID,
        MAX(tblClassificationHistory.reclassificationDate) AS max_date
        FROM tblinmate
        INNER JOIN tblClassificationHistory
        ON tblinmate.inmateID = tblClassificationHistory.inmateID
        GROUP BY tblClassificationHistory.inmateID
) AS b
ON (a.reclassificationDate = b.max_date)
AND (a.inmateID = b.inmateID)
ORDER BY a.inmateID;
 
Last edited:
I need data to help people. If you would provide sample data from your tables (along with table and field names) and then what you expect your query to return based on that sample data I can help you.
 
Sorry for that...i wrote a long post but it was so confusing trying to explain the relations etc so please download the attached DB and just run it. I have written an explanation in it and i think it will make it a lot clearer running it at the same time.
 

Attachments

If you have qryMain_dev and qrySquad working the way you want, you could create aother query based on them. Here is an example. You can change the fields you want to see to meet your needs.
Good luck

Code:
SELECT qrySquad.*
FROM qryMain_dev INNER JOIN qrySquad ON 
(qryMain_dev.max_date = qrySquad.max_date) AND 
(qryMain_dev.inmateID = qrySquad.inmateID);
 
If you have qryMain_dev and qrySquad working the way you want, you could create aother query based on them. Here is an example. You can change the fields you want to see to meet your needs.
Good luck

HAHA yeah that works and it isnt that hard to understand...i might need to add other "History" tables the same way makes that process easier...:D

But for my peace of mind ill still try to integrate the one in the other..i have to learn :p

**UPDATE
Im getting different amount of records in the queries...hmmm maybe my data is incomplete...
 
Last edited:
Different amount of records...the combination makes 13 records but each query by itself makes 18 records...a little strange. I saw i had some missing data fixed that but it didn't fix the record missmatch...attached a new DB ...

qryMainCombine 13 records
qryMain_dev 18 records
qryScuad 18 records

hmm.....strange...some inmateID's are missing....debugging...

**Update
The max_date in the queries are not related as they can be set seperatly only the inmateID is related in all the tables...
 

Attachments

Last edited:
Well I thought the queries were related which is not correct.

In plain English what exactly do you need? Your topic is Integrate query in query..

How exactly are these things related?

Also: I would not use lookups at the table field level.
 
sorry for the confusion...

I removed the relation between the max_date and its all well, at least that part.

What i wanted from the start was to integrate two queries into my main query.

I had managed to integrate one the ClassificationID Query into my main Query and was now trying to integrate the other SquadID query as well.

Maybe im using the wrong terminology as I presume they are called subQueries when they are inside another Query



Then you showed me this neat Combination method of Queries...so now im separating the queries again and then making a separate CombinationQuery. It makes everything so much easier to overview.

So for now im good :D it was just the max_date relation that was wrongly explained from my part...

Also: I would not use lookups at the table field level.

ps. I removed my lookup fields in the table level long ago...not sure where you saw them now...maybe a table not being used...
 
In table Squad, ClassificationName is a lookup into tblClassification;
tbl DisciplineRecord, inmateCase, MedicalRecord have lookups.

If there are relationships between all of the tables you are trying to get values from, then you should be able to do it in a query. Sometimes queries can get complex and it's easier to do it in steps.
 
In table Squad, ClassificationName is a lookup into tblClassification;
tbl DisciplineRecord, inmateCase, MedicalRecord have lookups.

Ahhhh yes you are absolutely right!

I havent gotten around to those yet. They are way down on my priority list :) I had all my tables with lookups before and removed them on I think it was yours or Gemmathehusks advice. But i havent started working on those tables/forms yet i left them as they where. But i promise i will get to those as well. I needed to sort out the main Query first and its done thanks to your nifty little Combination of Queries you showed me.

If there are relationships between all of the tables you are trying to get values from, then you should be able to do it in a query. Sometimes queries can get complex and it's easier to do it in steps.

Not sure if you mean that its advisable to combine queries like you showed me or if i should do it all in one like i was trying to...? All tables related as you can see in the relationstab in the DB
 
Sorry but I ran into a problem i hope its just quick solution...Now as i have changed the recordset to a query of combined queries...my recordset is not updable and thats a huge problem...is there a quick fix or do i have to remake it all somehow?


*Update
I understand it has to do that the result of the subQueries are derived tables... What could be a workaround for this..?
 
Last edited:
I hope you are working on a test copy of your database. If not, you should be.
You have to step back and be clear in what you are trying to do.

eg: I want to see all the inmates who are in block 6 or 9 who have had medical issues in the past 6 months and have a college degree, and show their latest medical date.

Then start building the query, piece by piece --checking each time to make sure the result is correct; then add more to the query. If necessary, pick 1 inmate/record that you can identify and use it to test/validate your query (eg inmateNo 999). Then expand to deal with any/all inmates.

Readers don't know the details of what you are doing nor your environment, so details for the query are important.
 
I hope you are working on a test copy of your database. If not, you should be.

:D Yes i am, this is my second version of the DB first one was kinda done then "someone" showed me the light about DB design/structure so i started this one.

You have to step back and be clear in what you are trying to do.
eg: I want to see all the inmates who are in block 6 or 9 who have had medical issues in the past 6 months and have a college degree, and show their latest medical date.

The need the jail has had, hasn't really been about long search string, more about some basic stats and input/reading single inmate information. Although i fully understand the benefits of being able to search like you showed. I'm just a little lost/scared with the new structure and all the complexity it has brought to the surface regarding queries, editing etc..

Then start building the query, piece by piece --checking each time to make sure the result is correct; then add more to the query. If necessary, pick 1 inmate/record that you can identify and use it to test/validate your query (eg inmateNo 999). Then expand to deal with any/all inmates

The whole DB is kinda based on a single Query. A mainform and 3 subforms on it representing the 3 other main tables. So its like "All" info at once no need to go looking anywhere its all there at a glance (Most of it). Meaning im not sure how "Then start building the query, piece by piece" applies to my DB

Readers don't know the details of what you are doing nor your environment, so details for the query are important.

Yeah I've been a little bad when it comes to that. Ill try to explain better


So back to my original Question or rather follow up to this Query in Query...because of the redesignen of the DB all following queries have become quite complicated and dont allow editing/adding and im getting a little cold feet if i should go back to my first "linear and most functions working but with flawed design db" or try to solve this one...

Attachment showing the first DB and the main functions its used for...
 

Attachments

  • CIPS-1st_version.jpg
    CIPS-1st_version.jpg
    93.6 KB · Views: 72
Last edited:
??What is a class
??What is a Squad
??What is an Aide position

These are not on the jpg showing the relationships etc.
 
Ahh ...sorry once again..

The redmarked missing items in the previous graphics pertain to the new DB structure attached here...

I wanted to design the new DB as much i could after the old one form wise..but that didnt work out really...as changing squad, Classification and Status etc meant editing/adding records in subforms on the mainform..(see my previously attached DB)

??What is a class
??What is a Squad
??What is an Aide position

class = ClassificationID
Squad = SquadID
Aide = AideTypeID
 

Attachments

  • relationsNew.gif
    relationsNew.gif
    45.8 KB · Views: 63
Last edited:

Users who are viewing this thread

Back
Top Bottom