(mirandacascade (AT) yahoo (DOT) com) writes:
Quote:
Questions:
1) it seems like there should be away to get the desired result set
using set-based queries (i.e not having to use a cursor) ... is there
a way to get the desired result set without using cursors?
2) it seems like I should be able to use the "TOP 5" clause in the
select statements, but I couldn't figure out how to implement a
solution using SELECT TOP 5 ... is there a way to make use of that? |
No, TOP will not cut it in this case. But dense_rank will:
WITH CTE AS (
SELECT CompanyName, IndustruCode, NetSales,
rank = dense_rank() OVER(PARTITION BY IndustryCode
ORDER BY NetSales DESC)
FROM tbl
)
SELECT CompanyName, IndustryCode, NetSales
FROM CTE
WHERE rank <= 5
ORDER BY IndustryCode, NetSales DESC
--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx