472,353 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,353 developers and data experts.

Ranking Function

debasisdas
8,127 Expert 4TB
Rank:-assigns A Unique Number For Each Row Starting With 1,except For Rows That Have Duplicate Values,in Which Case The Same Ranking Is Assigned And A Gap Appears In The Sequence For Each Duplicate Ranking.

Row_number:-returns A Unique Number For Each Row Starting With 1.for Rows That Have Duplicate Values,numbers Are Arbitarily Assigned.

Dense_rank:-assigns A Unique Number For Each Row Starting With 1,except For Rows That Have Duplicate Values ,in Which Case Same Ranking Is Assigned.

Ex #1
==========

Expand|Select|Wrap|Line Numbers
  1. select ename,empno ,sum(sal)  from emp where deptno=10 group by ename,empno order by ename,empno;
Ex #2
==========

Expand|Select|Wrap|Line Numbers
  1. select ename,empno ,sum(sal) SALARY,
  2. rank() over(order by sum (sal) desc) RANK,
  3. dense_rank() over(order by sum (sal) desc) SALARY_DENSE,
  4. ROW_NUMBER() OVER (order by sum (sal) desc) SALARY_NUMBER
  5. from emp
  6. where deptno=10
  7. group by ename,empno
  8. order by ename,empno;
Ex #3
==========
Expand|Select|Wrap|Line Numbers
  1. select ename,empno ,sum(sal) SALARY,
  2. rank() over (PARTITION BY ENAME order by sum (sal) desc) RANK,
  3. dense_rank() over (PARTITION BY ENAME order by sum (sal) desc) SALARY_DENSE,
  4. ROW_NUMBER() OVER (PARTITION BY ENAME order by sum (sal) desc) SALARY_NUMBER
  5. from emp
  6. group by ename,empno
  7. order by ename,empno;
Ex #4
==========
Expand|Select|Wrap|Line Numbers
  1. select DEPTNO,empno ,sum(sal) SALARY,
  2. rank() over (PARTITION BY DEPTNO order by sum (sal) desc) RANK,
  3. dense_rank() over (PARTITION BY DEPTNO order by sum (sal) desc) SALARY_DENSE,
  4. ROW_NUMBER() OVER (PARTITION BY DEPTNO order by sum (sal) desc) SALARY_NUMBER
  5. from emp
  6. WHERE DEPTNO=30
  7. group by DEPTNO,empno
  8. order by DEPTNO,empno;
Ex #5
==========
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. MIN(DEPTNO)
  3. KEEP (DENSE_RANK FIRST ORDER BY SUM(SAL) DESC) BEST,
  4. MIN(DEPTNO)
  5. KEEP (DENSE_RANK LAST ORDER BY SUM(SAL) DESC) WORST
  6. FROM EMP
  7. GROUP BY DEPTNO;
Ex #6
==========
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. MIN(DEPTNO)
  3. KEEP (DENSE_RANK FIRST ORDER BY SUM(SAL) DESC) MIN_BEST,
  4. MAX(DEPTNO)
  5. KEEP (DENSE_RANK FIRST ORDER BY SUM(SAL) DESC) MAX_BEST,
  6. MIN(DEPTNO)
  7. KEEP (DENSE_RANK LAST ORDER BY SUM(SAL) DESC) MIN_WORST,
  8. MAX(DEPTNO)
  9. KEEP (DENSE_RANK LAST ORDER BY SUM(SAL) DESC) MAX_WORST
  10. FROM EMP
  11. GROUP BY DEPTNO;
HANDLING NULLS
-----------------------------------
Expand|Select|Wrap|Line Numbers
  1. select DEPTNO,empno ,sum(sal) SALARY,
  2. rank() over (order by sum (sal) desc NULLS LAST) RANK
  3. from emp group by DEPTNO,empno;
  4.  
Jun 30 '07 #1
0 8548

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Petre Huile | last post by:
I have designed a site for a client, but they have hired an internet marketing person to incrase their search engine ranking and traffic. He wants...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba...
1
by: Joseph Bloch | last post by:
In all the threads on ranking in Access queries, I've not seen anything that deals with ranking against a subset of the query results. What I need...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it...
5
by: valglad | last post by:
Hi, The question below was posted about 4 years ago and noone was able to answer it back then. I have virtually the same type of problem so would...
6
by: sara | last post by:
I hope someone can help with this. Our director wants to have a report that will have the departments (Retail stores) across the top, stores down...
8
by: AnndieMac | last post by:
I have an Access 2002 database of inventory count results, and I have been asked to create summaries of the items with the most losses at a store,...
5
by: Chris | last post by:
I was wodering if there was a way to rank numbers in a query like this #'s Rank 100 1 99 2 98 3 98 97 5 96 6 96
0
by: Maciej Gawinecki | last post by:
Hello, Recently I've interested in quering XML document streams. They can be queried in keyword- based manner (non-structural queries), as the...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.