473,569 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get rank?

I would like to write a query that gives me the values of a set of
observations, and their rank.

CREATE TABLE #Values(val int)
INSERT #Values SELECT 1
INSERT #Values SELECT 5
INSERT #Values SELECT 10

I would like to select this:

1 10 -- rank 1, value 10
2 5
3 1

I can put them into a temp table with an identity column, ordered by
the column I'm interested in, and then retrieve in order by the
identity column. I'm wondering if there's a way to do that with a
subquery.

Thanks,
Jim

Jun 28 '06 #1
3 3848
Running count is one way

CREATE TABLE #Values(val int)
INSERT #Values SELECT 1
INSERT #Values SELECT 5
INSERT #Values SELECT 10

select (select count(*) from #Values v where val <= v2.Val) as Rank,*
from #Values v2
order by 2

Denis the SQL Menace
http://sqlservercode.blogspot.com/
jim_geiss...@co untrywide.com wrote:
I would like to write a query that gives me the values of a set of
observations, and their rank.

CREATE TABLE #Values(val int)
INSERT #Values SELECT 1
INSERT #Values SELECT 5
INSERT #Values SELECT 10

I would like to select this:

1 10 -- rank 1, value 10
2 5
3 1

I can put them into a temp table with an identity column, ordered by
the column I'm interested in, and then retrieve in order by the
identity column. I'm wondering if there's a way to do that with a
subquery.

Thanks,
Jim


Jun 28 '06 #2
Jim,

if you are using SS 2005 , use row_number() or rank() OLAP function

Jun 28 '06 #3
Where do you want to show data?
If you use front end application, do Ranking there

Madhivanan


ji**********@co untrywide.com wrote:
> I would like to write a query that gives me the values of a set of
observations, and their rank.

CREATE TABLE #Values(val int)
INSERT #Values SELECT 1
INSERT #Values SELECT 5
INSERT #Values SELECT 10

I would like to select this:

1 10 -- rank 1, value 10
2 5
3 1

I can put them into a temp table with an identity column, ordered by
the column I'm interested in, and then retrieve in order by the
identity column. I'm wondering if there's a way to do that with a
subquery.

Thanks,
Jim
Jul 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2968
by: Can | last post by:
I am creating an on-line survey. I want the user to have a list of choices of say 10 items that are radio buttons. They need to rank their preference. They click on preference 1, that option is removed from the top list (choices) and appears below in a list called 'Your preferences', you keep looping until all of your preferences are made....
3
2158
by: bughunter | last post by:
I discover next problem I have view definition with rank() create view vTEST as select c1, c2, c3, ... -- problem area start , rank() over (order by c3) as RNK -- problem area stop from table
3
24894
by: Elden Carnahan | last post by:
I am trying to derive rank values in a query, just as one would with the Rank function in Excel. Can't see how to do this elementary task. Can anyone advise?
2
1952
by: dam_fool_2003 | last post by:
Just now I asked a question about signed char and unsigned char. I had the replay very fast by jens.torring and Tim prince. I post via google so now goes my thanking replay. (I must be more precise according to jens.torring's replay) As I was reading the slandered draft C99 about the conversion (6.3) I have interpreted the word conversion...
3
1327
by: j0mbolar | last post by:
what's the rules of rank and how they affect types of expressions?
1
14285
by: Joe | last post by:
Has anyone written a percentile rank algorithm that they can share or know of a good resource that defines the algorithm. I'm starting a project that involves percentile ranks, logistic and linear regression. If not c# any language or pseudo code would be very useful. Thank you. Joe -- Sent via .NET Newsgroups...
13
5018
by: Steve Edwards | last post by:
Hi, Given a map: typedef map<long, string, greater<long> > mapOfFreq; Is there a quicker way to find the rank (i.e. index) of the elememt that has the long value of x? At the moment I'm iterating through the map and keeping count of when I hit it.
1
1670
by: volunteer | last post by:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="filename.xsl"?> <markers date="20060523"> 04:21:50 PM <marker sn="1" rank="6" name="john" /> <marker sn="2" rank="5" name="mary" /> <marker sn="3" rank="4" name=suzy" /> </markers> How do I sort by and display using etc- some help
2
2939
by: orenlevy1 | last post by:
Hi Everyone. I have a problem that I could not figure out what to do with it. I have a couple of tables and views. All have the same unique ID. When a user try to do a search on our web site we want to try to look in multiple tables and views, Rank the results and aggregate the results back (remove any duplicates). Part of the search is...
6
1854
by: canabatz | last post by:
Hello . i got a list of user with ranking . i want to display the first five places where it is not the same user, like that john : is rank 1 john john david : is rank 2 albert : is rank 3 albert
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.