I have a table(Orders) with 5 million records that also contains 250 unique groups(Markets). I am trying to apply a Top N query to retrieve the top 3 records(FirmNames), based on TotalVolume, from each unique group. My current query never finishes running. Is there a more efficient way to retrieve these top 3 records? Thanks
SELECT Orders.Market, Orders.TotalVolume, Orders.FirmName INTO [Top 3 Luxury Market Final]
FROM Orders
WHERE (((Orders.TotalVolume) In (SELECT TOP 3 TotalVolume
FROM Orders AS Dupe
WHERE Dupe.Market = Orders.Market
ORDER BY Dupe.TotalVolume DESC, Dupe.FirmName DESC)))
ORDER BY Orders.Market, Orders.TotalVolume DESC , Orders.FirmName;
SELECT Orders.Market, Orders.TotalVolume, Orders.FirmName INTO [Top 3 Luxury Market Final]
FROM Orders
WHERE (((Orders.TotalVolume) In (SELECT TOP 3 TotalVolume
FROM Orders AS Dupe
WHERE Dupe.Market = Orders.Market
ORDER BY Dupe.TotalVolume DESC, Dupe.FirmName DESC)))
ORDER BY Orders.Market, Orders.TotalVolume DESC , Orders.FirmName;