juddymar58
Programmer
Hi I have two tables
tblProjectLog
fldProjectID
fldProjectDesignPhaseID
fldLogDate
fldProjectIDLog - primary key
tblProjectDesignPhaseList
fldProjectDesignPhaseID - PK joins to same field in tblProjectLog
fldProjectDesignPhase - text (example of data 'Planning, 'Detailed Design' and 'Construction'
I need to return the earliest value of fldLogDate where fldProjectDesignPhase is 'Detailed Design' for each fldProjectID which is fairly easily using a group by query.
SELECT tblProjectLog.fldProjectID, Min(tblProjectLog.fldLogDate) AS fldFirstLogDate
FROM tblProjectLog INNER JOIN tblProjectDesignPhaseList ON tblProjectLog.fldProjectDesignPhaseID = tblProjectDesignPhaseList.fldProjectDesignPhaseID
WHERE (((tblProjectDesignPhaseList.fldProjectDesignPhase)="Detail Design"))
GROUP BY tblProjectLog.fldProjectID, tblProjectLog.fldProjectID
ORDER BY tblProjectLog.fldProjectID;
Where I'm stuck is I need to add a nested query to return the earliest log date where the design phase is 'Construction' for the project. I'm only the log date if the record is after
after the 'Detail Design' phase record. So I have tried to use fldProjectIDLog> original log value in query (to only use records that are after the original one).
I was hoping to have a nested query like the following but keep getting error messages I think the issue I'm having is passing in the grouped by values from the main queries into the nested part:
SELECT tblProjectLog.fldProjectID, Min(tblProjectLog.fldLogDate) AS fldFirstLogDate, (SELECT Min(tblProjectLog1.fldLogDate) AS fldMinLogDate, tblProjectDesignPhaseList1.fldProjectDesignPhase
FROM tblProjectLog AS tblProjectLog1 INNER JOIN tblProjectDesignPhaseList AS tblProjectDesignPhaseList1 ON tblProjectLog1.fldProjectDesignPhaseID = tblProjectDesignPhaseList1.fldProjectDesignPhaseID
WHERE tblProjectLog1.fldProjectID=[tblProjectLog].[fldProjectID] AND tblProjectDesignPhaseList1.fldProjectDesignPhase='Construction' AND tblProjectLog1.fldProjectIDLog>min([tblProjectLog].[fldLogDate])) AS fldFirstConstruction
FROM tblProjectLog INNER JOIN tblProjectDesignPhaseList ON tblProjectLog.fldProjectDesignPhaseID = tblProjectDesignPhaseList.fldProjectDesignPhaseID
WHERE (((tblProjectDesignPhaseList.fldProjectDesignPhase)="Detail Design"))
GROUP BY tblProjectLog.fldProjectID, (SELECT Min(tblProjectLog1.fldLogDate) AS fldMinLogDate, tblProjectDesignPhaseList1.fldProjectDesignPhase
FROM tblProjectLog AS tblProjectLog1 INNER JOIN tblProjectDesignPhaseList AS tblProjectDesignPhaseList1 ON tblProjectLog1.fldProjectDesignPhaseID = tblProjectDesignPhaseList1.fldProjectDesignPhaseID
WHERE tblProjectLog1.fldProjectID=[tblProjectLog].[fldProjectID] AND tblProjectDesignPhaseList1.fldProjectDesignPhase='Construction' AND tblProjectLog1.fldProjectIDLog>min([tblProjectLog].[fldLogDate]))
ORDER BY tblProjectLog.fldProjectID;
I know I could easily do this using multiple queries but I would like to become more familiar with nested queries. I have been searching nested queries and I'm fine when there is no grouping but I seem to be having issues as soon as I have to incorporate it.
Any advice appreciated.
Thanks
Justin
tblProjectLog
fldProjectID
fldProjectDesignPhaseID
fldLogDate
fldProjectIDLog - primary key
tblProjectDesignPhaseList
fldProjectDesignPhaseID - PK joins to same field in tblProjectLog
fldProjectDesignPhase - text (example of data 'Planning, 'Detailed Design' and 'Construction'
I need to return the earliest value of fldLogDate where fldProjectDesignPhase is 'Detailed Design' for each fldProjectID which is fairly easily using a group by query.
SELECT tblProjectLog.fldProjectID, Min(tblProjectLog.fldLogDate) AS fldFirstLogDate
FROM tblProjectLog INNER JOIN tblProjectDesignPhaseList ON tblProjectLog.fldProjectDesignPhaseID = tblProjectDesignPhaseList.fldProjectDesignPhaseID
WHERE (((tblProjectDesignPhaseList.fldProjectDesignPhase)="Detail Design"))
GROUP BY tblProjectLog.fldProjectID, tblProjectLog.fldProjectID
ORDER BY tblProjectLog.fldProjectID;
Where I'm stuck is I need to add a nested query to return the earliest log date where the design phase is 'Construction' for the project. I'm only the log date if the record is after
after the 'Detail Design' phase record. So I have tried to use fldProjectIDLog> original log value in query (to only use records that are after the original one).
I was hoping to have a nested query like the following but keep getting error messages I think the issue I'm having is passing in the grouped by values from the main queries into the nested part:
SELECT tblProjectLog.fldProjectID, Min(tblProjectLog.fldLogDate) AS fldFirstLogDate, (SELECT Min(tblProjectLog1.fldLogDate) AS fldMinLogDate, tblProjectDesignPhaseList1.fldProjectDesignPhase
FROM tblProjectLog AS tblProjectLog1 INNER JOIN tblProjectDesignPhaseList AS tblProjectDesignPhaseList1 ON tblProjectLog1.fldProjectDesignPhaseID = tblProjectDesignPhaseList1.fldProjectDesignPhaseID
WHERE tblProjectLog1.fldProjectID=[tblProjectLog].[fldProjectID] AND tblProjectDesignPhaseList1.fldProjectDesignPhase='Construction' AND tblProjectLog1.fldProjectIDLog>min([tblProjectLog].[fldLogDate])) AS fldFirstConstruction
FROM tblProjectLog INNER JOIN tblProjectDesignPhaseList ON tblProjectLog.fldProjectDesignPhaseID = tblProjectDesignPhaseList.fldProjectDesignPhaseID
WHERE (((tblProjectDesignPhaseList.fldProjectDesignPhase)="Detail Design"))
GROUP BY tblProjectLog.fldProjectID, (SELECT Min(tblProjectLog1.fldLogDate) AS fldMinLogDate, tblProjectDesignPhaseList1.fldProjectDesignPhase
FROM tblProjectLog AS tblProjectLog1 INNER JOIN tblProjectDesignPhaseList AS tblProjectDesignPhaseList1 ON tblProjectLog1.fldProjectDesignPhaseID = tblProjectDesignPhaseList1.fldProjectDesignPhaseID
WHERE tblProjectLog1.fldProjectID=[tblProjectLog].[fldProjectID] AND tblProjectDesignPhaseList1.fldProjectDesignPhase='Construction' AND tblProjectLog1.fldProjectIDLog>min([tblProjectLog].[fldLogDate]))
ORDER BY tblProjectLog.fldProjectID;
I know I could easily do this using multiple queries but I would like to become more familiar with nested queries. I have been searching nested queries and I'm fine when there is no grouping but I seem to be having issues as soon as I have to incorporate it.
Any advice appreciated.
Thanks
Justin