Solved

Top 20 produces Top 20

Posted on 2009-05-15
8
165 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:SeTech
  • 5
  • 3
8 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
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
0
 

Author Comment

by:SeTech
Comment Utility
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 ***
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
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.
0
 

Author Comment

by:SeTech
Comment Utility
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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
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

0
 

Author Comment

by:SeTech
Comment Utility
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. ???
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
Comment Utility
There is no reason to use a CTE since you aren't attempting to JOIN the CTE to itself.  I would just use a derived table.

If you must, I would create the view with the query tool and not the "new view" tool
Create View YOURVIEWNAME

as

;with topItems as (SELECT Row_Number() OVER (Partition BY Terr, Line ORDER BY Items DESC) AS theRank, Terr, Mkt, AgtNo, Items,  Line

From <ENTERYOURTABLENAMEHERE>)

Select theRank, Terr, Mkt, AgtNo, Items,  Line

from topItems

where theRank <= 20

 

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
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
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now