(I wish I could have come up with a more descriptive title… suggest one or edit this post if you can name the type of query I’m asking about)
Database: SQL Server 2000
Sample Data (assume 500,000 rows):
Name Candy PreferenceFactor Jim Chocolate 1.0 Brad Lemon Drop .9 Brad Chocolate .1 Chris Chocolate .5 Chris Candy Cane .5 499,995 more rows...
Note that the number of rows with a given ‘Name’ is unbounded.
Desired Query Results:
Jim Chocolate 1.0 Brad Lemon Drop .9 Chris Chocolate .5 ~250,000 more rows...
(Since Chris has equal preference for Candy Cane and Chocolate, a consistent result is adequate).
Question:
How do I Select Name, Candy from data where each resulting row contains a unique Name such that the Candy selected has the highest PreferenceFactor for each Name. (speedy efficient answers preferred).
What indexes are required on the table? Does it make a difference if Name and Candy are integer indexes into another table (aside from requiring some joins)?
1 Answer