SELECT CODE
, PAY_CONTRACT_CODE
, RATE
, EFFECTIVE_DATE
, DESTINATION_CODE
, PAY_ACTIVITY_CODE
, BLOCK_CODE
FROM [Truck rates] as T2
WHERE PAY_CONTRACT_CODE Like "TGH*"
AND DESTINATION_CODE Is Not Null
AND EffectiveDate =
(SELECT Max(EffectiveDate)
FROM [Truck Rates] as T1
WHERE T1.Block_Code = T2.Block_Code
AND T1.Destination_Code = T2.Destination_Code)
If that is too slow, then you could use a two query solution.
First query saved as "qLastEffective"
SELECT Destination_Code
, Block_Code,
, Max(Effective_Date) as LastDate
FROM [Truck Rates]
GROUP BY Destination_Code, Block_Code
Use that in a second query with the original table
SELECT CODE
, PAY_CONTRACT_CODE
, RATE
, EFFECTIVE_DATE
, DESTINATION_CODE
, PAY_ACTIVITY_CODE
, BLOCK_CODE
FROM [Truck rates] INNER JOIN qLastEffective
On [Truck Rates].Effective_Date = qLastEffective.LastDate
AND [Truck Rates].Destination_Code = qLastEffective.Destination_Code
AND [Truck Rates].Block_Code = qLastEffective.Block_Code
WHERE PAY_CONTRACT_CODE Like "TGH*"
AND DESTINATION_CODE Is Not Null
--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
Chris2 said:
Arlene said:
SELECT [Truck rates].CODE, [Truck rates].PAY_CONTRACT_CODE, [Truck
rates].RATE, [Truck rates].EFFECTIVE_DATE, [Truck rates].DESTINATION_CODE,
[Truck rates].PAY_ACTIVITY_CODE, [Truck rates].BLOCK_CODE
FROM [Truck rates]
WHERE ((([Truck rates].PAY_CONTRACT_CODE) Like "TGH*") AND (([Truck
rates].DESTINATION_CODE) Is Not Null));
I have the above query, there are a bunch of different rates for the same
blocks and destinations. I want to be able to have the query give me the
max, or most recent date on the rates, right now I get all the rates no
matter what the date. Can anyone help please. Thanks
Arlene,
Based on the information available, this is my best guess (untested).
SELECT [Truck rates].CODE
,[Truck rates].PAY_CONTRACT_CODE
,[Truck rates].RATE
,MAX([Truck rates].EFFECTIVE_DATE)
,[Truck rates].DESTINATION_CODE
,[Truck rates].PAY_ACTIVITY_CODE
,[Truck rates].BLOCK_CODE
FROM [Truck rates]
WHERE ((([Truck rates].PAY_CONTRACT_CODE) Like "TGH*")
AND (([Truck rates].DESTINATION_CODE) Is Not Null));
GROUP BY [Truck rates].CODE
,[Truck rates].PAY_CONTRACT_CODE
,[Truck rates].RATE
,[Truck rates].DESTINATION_CODE
,[Truck rates].PAY_ACTIVITY_CODE
,[Truck rates].BLOCK_CODE
Sincerely,
Chris O.