I'm trying to have only the records with the maximum (latest) date appear in my results. For some reason I am getting all dates for each result. What am I missing? I'm sure the issue is somewhere between LEFTOUTERJOIN and HAVING in the code below..
SELECT DISTINCT
tblDataPermit.ApplicationNumber AS [PermitAppNumber]
,tblDataPermit.PermitNumber
,tblDataPermit.Applicant
,tblDataPermit.Type
,tblDataPermit.ApplicationType
,tblDataPermit.XmlDataField.value('(//*[local-name()="ExtraNumber3"])[1]', 'nvarchar(max)') AS RUnits
,tblDataParcel.StreetName
,tblDataParcel.StreetNum
,tblDataParcel.Location
,SysCod.CodRcdTyp
,SysCod.CodLabel
,SysCod.CodOrder
,tblDataFees.ApplicationNumber AS [FeesAppNumber]
,tblDataFees.Amount
,tblDataFees.Date
FROM
SysCod
INNER JOIN tblDataPermit
ON SysCod.CodOrder = tblDataPermit.Type
INNER JOIN tblDataParcel
ON tblDataPermit.ParcelID = tblDataParcel.ParcelID
INNER JOIN tblDataFees
ON tblDataPermit.ApplicationNumber = tblDataFees.ApplicationNumber
**LEFT OUTER JOIN**
**(SELECT MAX(tblDataFees.Date) AS MaxFeesDate, tblDataFees.ApplicationNumber,tblDataFees.Amount**
**FROM tblDataFees**
**GROUP BY tblDataFees.ApplicationNumber, tblDataFees.Amount**
**HAVING (tblDataFees.Amount > '0')) As Amount**
**ON tblDataFees.ApplicationNumber = tblDataPermit.ApplicationNumber**
WHERE
tblDataPermit.Type = N'61'
AND tblDataPermit.XmlDataField.value('(//*[local-name()="ExtraNumber3"])[1]', 'nvarchar(6)') LIKE '%2'
AND tblDataFees.[Date] >= @Date
AND tblDataFees.[Date] <= @Date2
AND SysCod.CodRcdTyp = 'PMTMAS'