Hi Guys,
I have a table called tblPWTable. It has rates for County, and Effective Date. The rate I want is from a table called tblPWBenefits. It has County and Date Worked and Check Date. Either date would be used for extracting the rates, based on an option. Since Check Date will be the same for the period, the rates would be the same for County, however if Date Worked is used, a different rate by be extracted.
Phase I of my database used just Check Date, so I did a join between the two tables and no issues since tblPWTable only had one set of rates in the table. There is a need to process older Check Runs, plus the Date Worked option. This requires more than one row per County with a different effective date.
I created a query for tblPWTable and sorted by County, and then Effective Date, descending. This way if Check Date >= Effective Date, I have a match. I tried using DMax and Top 1 but no success (It's an update query, so Top 1 would be useless).
This is what I'm trying to do. Create a query, qryPWTableRange and add a column ToDate (if it's the first rate for the county, I set the ToDate to 12/21/2099, otherwise, I subtract 1 from ToDate), which should be the Effective Date from the previous row. But it's not working.
Once I get that query working, I can join tables using a date range.
I apologize for using images.
Here is the SQL for using TOP 1. TOP 1 is returning 2/1/2022 for all rows for ExDate (date from qryPWTable).
The Results Are:
![Name: Top1.jpg
Views: 9
Size: 44.5 KB]()
The SQL for DMax. DMax is returning the same date.
The Results Are:
![Name: DMax.jpg
Views: 9
Size: 44.6 KB]()
Thanks so much.
I have a table called tblPWTable. It has rates for County, and Effective Date. The rate I want is from a table called tblPWBenefits. It has County and Date Worked and Check Date. Either date would be used for extracting the rates, based on an option. Since Check Date will be the same for the period, the rates would be the same for County, however if Date Worked is used, a different rate by be extracted.
Phase I of my database used just Check Date, so I did a join between the two tables and no issues since tblPWTable only had one set of rates in the table. There is a need to process older Check Runs, plus the Date Worked option. This requires more than one row per County with a different effective date.
I created a query for tblPWTable and sorted by County, and then Effective Date, descending. This way if Check Date >= Effective Date, I have a match. I tried using DMax and Top 1 but no success (It's an update query, so Top 1 would be useless).
This is what I'm trying to do. Create a query, qryPWTableRange and add a column ToDate (if it's the first rate for the county, I set the ToDate to 12/21/2099, otherwise, I subtract 1 from ToDate), which should be the Effective Date from the previous row. But it's not working.
Once I get that query working, I can join tables using a date range.
I apologize for using images.
Here is the SQL for using TOP 1. TOP 1 is returning 2/1/2022 for all rows for ExDate (date from qryPWTable).
Code:
SELECT tblPWTable.County, tblPWTable.[Trade Title], tblPWTable.[Effective Date], (SELECT TOP 1 qryPWTable.[Effective Date]
FROM qryPWTable
WHERE tblPWTable.[Effective Date] >=
qryPWTable.[Effective Date] AND
tblPWTable.County = qryPWTable.County AND
tblPWTable.[Trade Title] = qryPWTable.[Trade Title]
ORDER BY tblPWTable.County, tblPWTable.[Trade Title], tblPWTable.[Effective Date] DESC
) AS ExDate, IIf([tblPWTable].[Effective Date]=[ExDate],#12/31/2099#,[ExDate]-1) AS ToDate, tblPWTable.[Base Wage], tblPWTable.[H/W], tblPWTable.Pension, tblPWTable.Vacation, tblPWTable.Training, tblPWTable.[Other Fringe Benefit], tblPWTable.[Total Fringe Benefits], tblPWTable.State
FROM tblPWTable
ORDER BY tblPWTable.County, tblPWTable.[Trade Title], tblPWTable.[Effective Date] DESC;
The SQL for DMax. DMax is returning the same date.
Code:
SELECT tblPWTable.County, tblPWTable.[Trade Title], tblPWTable.[Effective Date], qryPWTable.[Effective Date] AS ExDate,
IIf([tblPWTable].[Effective Date]=[qryPWTable].[Effective Date],#12/31/2099#,[qryPWTable].[Effective Date]-1) AS ToDate,
tblPWTable.[Base Wage], tblPWTable.[H/W], tblPWTable.Pension, tblPWTable.Vacation, tblPWTable.Training, tblPWTable.[Other Fringe Benefit], tblPWTable.[Total Fringe Benefits], tblPWTable.State
FROM tblPWTable INNER JOIN qryPWTable ON (tblPWTable.[Trade Title] = qryPWTable.[Trade Title]) AND (tblPWTable.County = qryPWTable.County)
WHERE (((qryPWTable.[Effective Date])=DMax("[Effective Date]","qryPWTable","[qryPWTable].[Effective Date]<=#" & [tblPWTable].[Effective Date] & "#")))
ORDER BY tblPWTable.County, tblPWTable.[Trade Title], tblPWTable.[Effective Date] DESC;
Thanks so much.