Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Top 1 per each customer

Author  Topic 

sigmas
Posting Yak Master

172 Posts

Posted - 2013-07-17 : 15:13:48
Is there an efficient index for this query?

use northwind
Select orderid, customerid, employeeid, orderdate, RequiredDate
From
(
Select dense_rank() over(partition by employeeID order by orderdate desc) as rnk,
*
From orders
)d
Where rnk = 1;

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2013-07-17 : 15:49:04
possibly employeeID, orderdate desc
but depends on how much data you have and what else is going on.
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2013-07-17 : 18:14:24
What are you hoping for? You can't answer that query without a scan on either an index or a table.
Go to Top of Page
   

- Advertisement -