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'
WITH Fees
AS
(
SELECT ApplicationNumber, Amount, [Date]
,RANK() OVER (PARTITION BY ApplicationNumber ORDER BY [Date] DESC) AS rn
--,RANK() OVER (PARTITION BY ApplicationNumber, Amount ORDER BY [Date] DESC) AS rn
FROM tblDataFees
)
SELECT --DISTINCT
P.ApplicationNumber AS [PermitAppNumber]
,P.PermitNumber
,P.Applicant
,P.[Type]
,P.ApplicationType
,P.XmlDataField.value('(//*[local-name()="ExtraNumber3"])[1]', 'nvarchar(max)') AS RUnits
,R.StreetName
,R.StreetNum
,R.Location
,C.CodRcdTyp
,C.CodLabel
,C.CodOrder
,F.ApplicationNumber AS [FeesAppNumber]
,F.Amount
,F.[Date]
FROM SysCod C
INNER JOIN tblDataPermit P
C.CodOrder = P.[Type]
INNER JOIN tblDataParcel R
ON P.ParcelID = R.ParcelID
INNER JOIN Fees F
ON P.ApplicationNumber = F.ApplicationNumber
AND F.rn = 1
WHERE P.[Type] = N'61'
AND P.XmlDataField.value('(//*[local-name()="ExtraNumber3"])[1]', 'nvarchar(6)') LIKE N'%2'
AND F.[Date] BETWEEN @Date AND @Date2
AND C.CodRcdTyp = 'PMTMAS';
Hi
Hope this helps
# Latest Records Query
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 ApplicationNumber, MAX(Date) AS MaxFeesDate
FROM tblDataFees
WHERE Amount > '0'
GROUP BY ApplicationNumber) AS LatestFees
ON tblDataFees.ApplicationNumber = LatestFees.ApplicationNumber
AND tblDataFees.Date = LatestFees.MaxFeesDate
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'