I am refactoring some code that was written by another person that is no longer around. In this query a MAX(Date) is being selected, but then joined to they same table via MAX(Locator). The locator is an arbitrary sequential number that is assigned when a record is created, so essentially the MAX(Locator) would return the most recent record the same as MAX(Date). Is there any purpose to this seeming redundancy?
SELECT DISTINCT
MAX(T.USERDATE2) AS BillPayDate
INTO
#PersonBillPayAccounts
FROM
[TRACKING] T
JOIN
(SELECT ACCOUNT_NUMBER, MAX(Locator) AS Locator
FROM [TRACKING]
WHERE Type = 32
AND (userdate2 IS NOT NULL OR userdate2 != '')
GROUP BY ACCOUNT_NUMBER) L ON T.ACCOUNT_NUMBER = L.ACCOUNT_NUMBER
AND T.LOCATOR = L.Locator
AND T.Type = 32
T.Type = 32in all of this ??SELECT MAX(LOCATOR) maxLOC, max(Userdate2) maxBillPayDate, Account_number FROM TRACKING WHERE Type = 32 and userdate2 is not null or userdate2 !='' GROUP BY Account_number EXCEPT SELECT LOCATOR, Userdate2, Account_number FROM TRACKING WHERE Type = 32 and userdate2 is not null or userdate2 !=''and get any records back, then you have records whose bill date doesn't align with the max locator. First query gets max date bill pay and by account. Second query returns each record as is so result will show if you made a bad assumption.