|
Hi there, Can anyone tell me if there is a more prudent and efficient way of rewriting the following SQL? TIA
(comments are locked)
|
|
I'm not too sure about how the last enquiry agent column (if there is more than one hit for a client and date it will return more than one row), but the rest should be fine (Table scripts and test data would help out with testing): Basically, CASE is your friend. You run through the table with one pass collecting all the different MAX() dates using case to decide if a row matches or not. You then join back to your client table. The last left join is to get the agent for the max(createdate) we found in the aggregate subquery. This would all be much nicer if you had SQL 2005 or newer, as the window functions would do that nicely. Thank you my friend. So CASE passes through the table once but these sub queries would go pass through one time each? I'll test and let you know. Much appreciated.
Aug 30 '11 at 08:04 AM
David 2 1
Your original sub-queries are correlated sub-queries. This means that they will be run for each row in the client table (the queries rely on the data supplied from the client table). My version will run once through the log table, collect all the info needed and then join back to the client table, this should be more efficient as it accesses the log table only once.
Aug 30 '11 at 11:57 AM
WilliamD
(comments are locked)
|

