Link to home
Start Free TrialLog in
Avatar of SeTech
SeTechFlag for United States of America

asked on

Top 20 produces Top 20

The code below produces my desired result of using the Row_Number () function to Rank a number of agents within  their Sales Territory (Partitioned by Terr) by each Line of business (2nd Partition Line) using Items Sold to Order them from high to low (best to worse within each line). There are 5 Territories and 5 Liness of business.

The query works as there are about 1200 agents with 5 lines of business and my result contains 6000 rows all in descending order by Terrr. I am happy. However, I am being ask to supply only the Top 20 for each terr by line of business. So I tried to a Select Top 20 and that returns the Top 20 rows in the query.

Any suggestions, perhaps a stored procedure?
 
SELECT     TOP 100 PERCENT Row_Number() OVER (Partition BY Terr, Line
ORDER BY Items DESC) AS Rank, Terr, Mkt, AgtNo, Items,  Line

Open in new window

Avatar of BrandonGalderisi
BrandonGalderisi
Flag of United States of America image

select * from
(
SELECT     TOP 100 PERCENT Row_Number() OVER (Partition BY Terr, Line
ORDER BY Items DESC) AS Rank, Terr, Mkt, AgtNo, Items,  Line
From SOMETABLE
)a
where [Rank] >= 20
Avatar of SeTech

ASKER

Brandon - So I kept trying and found that a CTE works do the
with top as (SELECT     TOP 100 PERCENT Row_Number() OVER (Partition BY Terr, Line
ORDER BY Items DESC) AS Rank, Terr, Mkt, AgtNo, Items,  Line)
Select Rank, Terr, Mkt, AgtNo, Items,  Line
from Top
where Rank <= 20

*** Similar to your solutions, which I think is a dervied table when using a select in the from ***
Correct.  You can use a CTE or a derived table.  The cases in which I would use a CTE is if you have the need to reference the CTE more than one time in the select statement.  Otherwise, I would use a derived table as I have done.  

For your case, they do exactly the same.
Avatar of SeTech

ASKER

Brandon - thanks much - I have encountered an issue with the CTE. I have it in a view (SQL 2005) and it will not save the With Nmae As portion of the CTE. This being the very begining prior to the  (Select. So what happens is I save and loose that portion. Any ideas?

Thank you
In general, it is a bad idea to use SQL Server key words, or reserved words, in your object/column names.

The following in the below code violate that.
rank - column name
top - CTE name

You can code around this by encapsulating it in brackets [] or by changing the name.  If you aren't SET on these names yet, I suggest changing it and using my first query below.  If you must, then use the second.

I changed the name of the CTE from top to topItems as it is not referenced outside of the view.
;with topItems as (SELECT     TOP 100 PERCENT Row_Number() OVER (Partition BY Terr, Line
ORDER BY Items DESC) AS theRank, Terr, Mkt, AgtNo, Items,  Line)
Select theRank, Terr, Mkt, AgtNo, Items,  Line
from topItems
where theRank <= 20
 
 
;with topItems as (SELECT     TOP 100 PERCENT Row_Number() OVER (Partition BY Terr, Line
ORDER BY Items DESC) AS Rank, Terr, Mkt, AgtNo, Items,  Line)
Select [Rank], Terr, Mkt, AgtNo, Items,  [Line]
from topItems
where Rank <= 20

Open in new window

Avatar of SeTech

ASKER

I understand about the key words and corrected. However the problem is (and using your code) the with topItems as - this part of the code does NOT save when I close my view. If I use the ; in fornt it will not parse and kicks me out. ???
ASKER CERTIFIED SOLUTION
Avatar of BrandonGalderisi
BrandonGalderisi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, I see you are doing top 100 percent.  I hope you weren't doing that in an attempt to ORDER within your view.  I've written up an article on why that doesn't work if you have some time to read.

http://sqlservernation.com/blogs/brandongalderisi/archive/2009/03/15/when-ordering-in-a-view-doesn-t-work.aspx