Selecting SUM of TOP 2 values within a table with multiple GROUP in SQL

后端 未结 3 566
难免孤独
难免孤独 2021-01-12 03:06

I\'ve been playing with sets in SQL Server 2000 and have the following table structure for one of my temp tables (#Periods):

    RestCTR     HoursCTR    Duration          


        
3条回答
  •  -上瘾入骨i
    2021-01-12 03:22

    Unfortunately for you, Alex, you've got the right solution: correlated subqueries, depending upon how they're structured, will end up firing multiple times, potentially giving you hundreds of individual query executions.

    Put your current solution into the Query Analyzer, enable "Show Execution Plan" (Ctrl+K), and run it. You'll have an extra tab at the bottom which will show you how the engine went about the process of gathering your results. If you do the same with the correlated subquery, you'll see what that option does.

    I believe that it's likely to hammer the #Periods table about as many times as you have individual rows in that table.

    Also - something's off about the correlated subquery, seems to me. Since I avoid them like the plague, knowing that they're evil, I'm not sure how to go about fixing it up.

提交回复
热议问题