I am fairly new to SQL and am looking for a solution. I am looking to find the best way to get the newest date from transaction date while being grouped by Job reference. Since these are different tables I am struggling someone please help.
SELECT
C.CustomerCode,
C.name AS Customer,
B1.Name AS 'Customer Home Branch',
B.Name AS 'Trx Branch',
At.DocumentNumber AS 'Document
Number',
At.TransactionDate AS 'Document
Date',
At.PaymentDueDate AS 'Due Date',
CASE
WHEN At.TransactionType = 1 THEN 'Invoice'
WHEN at.transactiontype = 2 THEN 'CreditNote'
WHEN at.transactiontype = 3 THEN 'FC'
WHEN AT.transactiontype = 4 THEN 'Payment'
WHEN At.TransactionType = 8 THEN 'Adj'
ELSE ''
END AS Type,
CA.AddressCode,
J.JobReference,
At.OriginalAmount AS 'Original
Amount',
At.AmountOutstanding AS 'Outstanding Amount',
DATEDIFF(DAY, AT.PaymentDueDate, GETDATE()) AS 'Days Past Due',
DATEDIFF(DAY, At.TransactionDate, GETDATE()) AS 'Days From Shipment',
C.customerID,
AT.AccountsTransactionID
FROM
AccountsTransaction AS AT WITH (NOLOCK)
LEFT JOIN
Customer AS C WITH(NOLOCK) ON C.customerid = AT.CustomerID
LEFT JOIN
Branch AS B WITH(NOLOCK) ON B.branchid = AT.BranchID
LEFT JOIN
Branch AS B1 WITH(NOLOCK) ON B1.branchid = C.HomeBranchID
LEFT JOIN
CustomerAddress AS CA WITH(NOLOCK) ON CA.customerAddressID = AT.DeliveryAddressID
LEFT JOIN
Job AS J WITH(NOLOCK) ON j.jobID = CA.JobID
Please help!! I am having a very hard time using a max and group by with so many tables being joined above. I would like to group by job reference and show the newest transaction date in this manner.
INNER JOIN
(SELECT
AT.DeliveryAddressID,
MAX('TransactionDate') AS ND
FROM
AccountsTransaction AS AT
GROUP BY
AT.DeliveryAddressID) AS New ON AT.DeliveryAddressID = J.JobID