So, here's a query that should produce the table layout. I changed the grouping order to Computer, Disk, then reading date, because I expect that will give Jet an efficiency advantage in all the joins by Computer and Disk:
Code:
Query qryUsageDeltas:
SELECT
DS1.Computer_Name,
DS1.Disk_ID,
DS1.Date_Of_Report As CurrentDate,
DS2.Date_Of_Report As PriorDate,
(DS1.Date_Of_Report - DS2.Date_Of_Report) As ChgInDate,
DS1.Disk_Size_MB As CurrentSize,
DS2.Disk_Size_MB As PriorSize,
(DS1.Disk_Size_MB - DS2.Disk_Size_MB) As ChgInSize,
DS1.Percentage_Used AS CurrentPct,
DS2.Percentage_Used As PriorPct,
(DS1.Percentage_Used - DS2.Percentage_Used) As ChgInPct,
DS1.Disk_Space_Used As CurrentUsed,
DS2.Disk_Space_Used As PriorUsed,
(DS1.Disk_Space_Used - DS2.Disk_Space_Used) As ChgInUsed
FROM
DiskSpaceStats AS DS1
INNER JOIN
DiskSpaceStats AS DS2
ON DS2.Computer_Name = DS1.Computer_Name
AND DS2.Disk_ID = DS1.Disk_ID
WHERE DS2.Date_Of_Report =
(SELECT MAX(DS3.Date_Of_Report)
FROM DiskSpaceStats AS DS3
WHERE DS1.Computer_Name = DS3.Computer_Name
AND DS1.Disk_ID = DS3.Disk_ID
AND DS1.Date_Of_Report > DS3.Date_Of_Report)
ORDER BY
DS1.Computer_Name,
DS1.Disk_ID,
DS1.Date_Of_Report DESC;
And here's the query that should use that to make your desired report:
Code:
query qryEstFullDate:
SELECT
Q1.CurrentDate,
Q1.Computer_Name,
Q1.Disk_ID,
Q1.CurrentSize,
Q1.CurrentUsed,
(Q1.CurrentSize - Q1.CurrentUsed) AS CurrentLeft,
Q2.DailyDelta,
(CurrentLeft/Q1.DailyDelta) AS EstDaysLeft,
DateAdd("d",EstDaysLeft,Q1.CurrentDate) AS EstFullDate
FROM
qryUsageDeltas AS Q1
INNER JOIN
(SELECT
Q2.Computer_Name,
Q2.Disk_ID,
AVG(Q2.ChgInUsed/Q2.ChangeInDate) AS DailyDelta
FROM qryUsageDeltas AS Q2
GROUP BY Q2.Computer_Name, Q2.Disk_ID
)
ON Q1.Computer_Name = Q2.Computer_Name
AND Q1.Disk_ID = Q2.Disk_ID
WHERE Q1.CurrentDate =
(SELECT Max(Q3.CurrentDate)
From qryUsageDeltas AS Q3
WHERE Q1.Computer_Name = Q3.Computer_Name
AND Q1.Disk_ID = Q3.Disk_ID);
FIX NOTES:
I made a fix to the joins in the query here, and put the aliases in order Q1, Q2, Q3. The previous references to TD2 and TD3 were inconsistent - no wonder you were confused.
I also recently discovered that we can re-use calculated fields in other calculations in a single query, so this version should read much easier.
EFFICIENCY NOTES:
Now, honestly, both of those are still aircode and I'm not sure how efficiently those queries will operate together.
Since the first query has two correlated subqueries to DiskSpaceStats, and then the second refers to the first query three times, the Jet database engine may be joining that DiskSpaceStats table to itself eight or more times. I'd expect Jet to be more efficient than that, creating the query and then using the intermediate result three times, but that may depend on the indexing of the underlying DiskSpaceStats table.
Hmmm. Q3 should probably be switched to DiskSpaceStats in either case, since only the date of the latest DiskSpaceStats record for each Computer/Disk combination is needed. That would look like this:
Code:
query qryEstFullDate:
SELECT
Q1.CurrentDate,
Q1.Computer_Name,
Q1.Disk_ID,
Q1.CurrentSize,
Q1.CurrentUsed,
(Q1.CurrentSize - Q1.CurrentUsed) AS CurrentLeft,
Q2.DailyDelta,
(CurrentLeft/Q1.DailyDelta) AS EstDaysLeft,
DateAdd("d",EstDaysLeft,Q1.CurrentDate) AS EstFullDate
FROM
qryUsageDeltas AS Q1
INNER JOIN
(SELECT
Q2.Computer_Name,
Q2.Disk_ID,
AVG(Q2.ChgInUsed/Q2.ChangeInDate) AS DailyDelta
FROM qryUsageDeltas AS Q2
GROUP BY Q2.Computer_Name, Q2.Disk_ID
)
ON Q1.Computer_Name = Q2.Computer_Name
AND Q1.Disk_ID = Q2.Disk_ID
WHERE Q1.CurrentDate =
(SELECT Max(T3.Date_Of_Report)
From DiskSpaceStats AS T3
WHERE Q1.Computer_Name = T3.Computer_Name
AND Q1.Disk_ID = T3.Disk_ID);
The first version would be more efficient if there were an actual table of tblUsageDeltas, but the last version would perhaps be more efficient if it's a query. If performance on these queries turns out to be an issue, then check back and we can see about optimizing more.