?
Solved

increment a rank/count on identical record values?

Posted on 2005-03-23
2
Medium Priority
?
500 Views
Last Modified: 2012-05-05
I've run into a `new` problem from the ranking query originally posted here:
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21333108.html

the current state of the query is this:
SELECT [Agt],[Terr],[Pol],
(Select Count(*)+1 from tablename Where Agt = t.Agt and (Pol > t.Pol or (Pol=t.Pol and Terr < t.Terr and Pol > t.Pol))) As "Rank"
FROM tablename t
ORDER BY Agt,Rank,Terr,Pol

prior data sets looked and worked great:
AGT        TERR POL   RANK
5010AB    12     70       1
5010AB    11     17       2
5010AB    10     7        3
5010AB    13     1        4
6610CC    22     59       1
6610CC    21     57       2
6610CC    10     37       3

but a new conundrum came up:
AGT        TERR POL   RANK
5010AB    12     70       1
5010AB    10     17       2
5010AB    11     17       2
5010AB    13     5        3

while I desire this as results:
AGT        TERR POL   RANK
5010AB    12     70       1
5010AB    10     17       2
5010AB    11     17       3
5010AB    13     5        4

how can I modify the above query to still rank in descending order,
and where POL is identical
rank the lowest TERR first
and increment the RANK on the next TERR, etc.?

thanks!
0
Comment
Question by:jwa1107
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 13612101
SELECT [Agt],[Terr],[Pol],
(Select Count(*)+1 from tablename Where Agt = t.Agt and (Pol > t.Pol or (Pol=t.Pol and Terr < t.Terr))) As "Rank"
FROM tablename t
ORDER BY Agt,Rank,Terr,Pol
0
 
LVL 4

Expert Comment

by:antonbijl
ID: 13612118
Maybe I'm missing something, but (Pol=t.Pol and Terr < t.Terr and Pol > t.Pol) will never be true because if Pol=t.Pol and Pol>t.Pol are mututally exclusive. I'm not 100% sure I understand fully what you're trying to accomplish but I think you need to remove that last Pol>t.Pol, like so:

SELECT [Agt],[Terr],[Pol],
(Select Count(*)+1 from tablename Where Agt = t.Agt and (Pol > t.Pol or (Pol=t.Pol and Terr < t.Terr))) As "Rank"
FROM tablename t
ORDER BY Agt,Rank,Terr,Pol
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

764 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