2015-09-17 49 views
-1

下面的查詢中的「Last」函數(第4行& 5)與我正在使用的不完全相同。最後一個函數查找該表中的最後一條記錄。訪問:將子查詢集成到激發查詢中

我需要查找的是根據日期字段表中最近的記錄。

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」功能

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; 

回答

-1

我從另一個論壇上尖查詢組合這樣

SELECT qryMainTemp.*, qrySquad.*, qryClassification.* 
FROM (qryMainTemp INNER JOIN qrySquad ON qryMainTemp.inmateID = qrySquad.inmateID) INNER JOIN qryClassification ON qryMainTemp.inmateID = qryClassification.inmateID; 

它的工作:)我把第一個查詢分成兩個查詢它是由,然後結合上面所示的三個。

不幸的是,這使得另一個問題出現了,查詢現在不可升級。在此解決方案上工作