I'm working on aging report for accounts over 30 days and I am joining the "Comment" table because AP wants to see the most recent comment if it exists for a customer.
The comment table can have multiple rows for the same customer for each comment that is attached to that customer with the first record being the oldest comment.
When I join the tables everything works, but it returns the first comment not the latest.
How can I get it to to look for multiple rows for the same customer and then return the most recent comment?
*Note - The comment table does not have dates just a field the starts at 1000 and increases for each new comment row
This is what i have right now:
SELECT
dbo.[Pioneer-CO$Purchase Header].No_,
dbo.[Pioneer-CO$Purchase Line].[Amt_ Rcd_ Not Invoiced],
dbo.[Pioneer-CO$Purch_ Rcpt_ Header].[Posting Date],
dbo.[Pioneer-CO$Comment Line].[Comment],
dbo.[Pioneer-CO$Purchase Header].[Sell-to Customer No_]
FROM
dbo.[Pioneer-CO$Purchase Header]
INNER JOIN
dbo.[Pioneer-CO$Purchase Line] ON dbo.[Pioneer-CO$Purchase Header].No_ = dbo.[Pioneer-CO$Purchase Line].[Document No_]
INNER JOIN
dbo.[Pioneer-CO$Purch_ Rcpt_ Header] ON dbo.[Pioneer-CO$Purchase Header].No_ = dbo.[Pioneer-CO$Purch_ Rcpt_ Header].[Order No_]
INNER JOIN
dbo.[Pioneer-CO$Comment Line] ON dbo.[Pioneer-CO$Purchase Header].[Sell-to Customer No_] = dbo.[Pioneer-CO$Comment Line].[No_]
WHERE
(dbo.[Pioneer-CO$Purch_ Rcpt_ Header].[Posting Date] < DATEADD(day, - 30, GETDATE()))
GROUP BY
dbo.[Pioneer-CO$Purchase Header].No_,
dbo.[Pioneer-CO$Purchase Line].[Amt_ Rcd_ Not Invoiced],
dbo.[Pioneer-CO$Purch_ Rcpt_ Header].[Posting Date],
dbo.[Pioneer-CO$Comment Line].[Comment],
dbo.[Pioneer-CO$Purchase Header].[Sell-to Customer No_]
HAVING
(dbo.[Pioneer-CO$Purchase Line].[Amt_ Rcd_ Not Invoiced] > '0')
ORDER BY
dbo.[Pioneer-CO$Purch_ Rcpt_ Header].[Posting Date] DESC