Timely insight needed!
Been tasked with trouble shooting a archived sql script to ensure that it is extracting the correct data
(The person who created the sql script is not available...)
Relatively new to Sql Server sql scripts involving "ROW_NUMBER() OVER PARTITION."
What exactly is the sql script performing?
Based on a cursory review, it appears that it is extracting the 100 distinct account numbers where the Load_Date is the most recent.
Is this correct?
Why is "ROW_NUMBER() OVER PARTITION" used? Can this be accomplished by another method?
Of course, I am running the sql script via Management Studio but not readily able to determine if the Sql script can be simplified at this point.
Thanks in advance.
Been tasked with trouble shooting a archived sql script to ensure that it is extracting the correct data
(The person who created the sql script is not available...)
Relatively new to Sql Server sql scripts involving "ROW_NUMBER() OVER PARTITION."
What exactly is the sql script performing?
Based on a cursory review, it appears that it is extracting the 100 distinct account numbers where the Load_Date is the most recent.
Is this correct?
Why is "ROW_NUMBER() OVER PARTITION" used? Can this be accomplished by another method?
Of course, I am running the sql script via Management Studio but not readily able to determine if the Sql script can be simplified at this point.
Thanks in advance.
Code:
SELECT AccountNo, Max, Bonus
FROM (SELECT TOP (100) PERCENT AccountNo, Max, SUM(Bonus) AS Bonus, ROW_NUMBER() OVER (PARTITION BY [AccountNo] ORDER BY AccountNo, Max DESC) AS Row
FROM (SELECT DISTINCT TOP (100) PERCENT AccountNo, ROUND(Bonus, 0) AS Bonus, MAX(Load_Date) AS Max
FROM dbo.Sales AS TMC
GROUP BY AccountNo, ROUND(Bonus, 0)
ORDER BY AccountNo) AS Tmc1
GROUP BY AccountNo, Max
ORDER BY AccountNo, Max DESC) AS Tmc2
WHERE (Row = 1)
ORDER BY AccountNo