Skip to Main Content
  • Questions
  • Create linenumbers for sqlplus queries on the fly.

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Douglas.

Asked: January 13, 2017 - 3:31 pm UTC

Last updated: January 14, 2017 - 7:47 am UTC

Version: 11.2.0.4

Viewed 1000+ times

You Asked

Is there a quick an easy way to create sequential line numbers on the fly (without Creating a Sequence or using PL*SQL) for a SQLplus query executed inside SQLplus?
Example:

Here is a live link to output:
http://www.videofame.com/fragrance.asp

SELECT rownum,C_COMMON_NAME,substr(C_MANUFACTURE_NAME,1,21),C_COUNTRY_MANUFACTURE
from cologne_master
order by C_COMMON_NAME

Yields This
23 BelAmi Hermis FRANCE
32 Bijan for Men Bijan USA
15 CH for Men Charolina Herrera SPAIN
2 Carnal Flower Frederic Malle FRANCE
35 Chaleur D Animale Chaleur USA
40 Chambre Noire Olfactive Studio FRANCE
33 Chaps Ralph Lauren USA
25 DV Duc De Vervins FRANCE
27 Dolce Gabana Men Dolce Gabana FRANCE
20 Fraicheur Menthe Pasha de Cartier FRANCE
30 Gautier 2 Jean Paul Gautier FRANCE
36 Givenchy Gentleman Givenchy FRANCE
31 Givenchy Pie Givenchy FRANCE

as you can see there definitely are line number but there in a crazy sequence. I want the sort order to look like example below with the line numbers displaying 1-16 in sequential order

1 BelAmi Hermis FRANCE
2 Bijan for Men Bijan USA
3 CH for Men Charolina Herrera SPAIN
4 Carnal Flower Frederic Malle FRANCE
5 Chaleur D Animale Chaleur USA
6 Chambre Noire Olfactive Studio FRANCE
7 Chaps Ralph Lauren USA
8 DV Duc De Vervins FRANCE
9 Dolce Gabana Men Dolce Gabana FRANCE
10 Fraicheur Menthe Pasha de Cartier FRANCE
11 Gautier 2 Jean Paul Gautier FRANCE
12 Givenchy Gentleman Givenchy FRANCE
13 Givenchy Pie Givenchy FRANCE

and Chris said...

If you want a line number, use the row_number() with the same order by as in your main query:

create table t as
  select dbms_random.string('a', 10) y 
  from dual connect by level <= 10;

select rownum, y, row_number() over (order by y) rn
from   t
order  by y;

ROWNUM  Y           RN  
5       EUKmelzlob  1   
10      JOgLmNyDwZ  2   
2       SqfQTVqevy  3   
7       jCHOIdsKuX  4   
4       jYqTLBfeEY  5   
1       kfvwyqSjYo  6   
3       leDMxUJXKs  7   
6       sViDShAFUI  8   
8       tvzGkPPCxm  9   
9       wBtsvBGqYg  10

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Totally Great and Quick Response

Douglas Call, January 13, 2017 - 4:19 pm UTC

Went ahead and did the substitution you suggested:
SELECT row_number() over (order by C_COMMON_NAME) rnum,
C_COMMON_NAME,substr(C_MANUFACTURE_NAME,1,21),C_COUNTRY_MANUFACTURE
from cologne_master
order by C_COMMON_NAME

and immediately got the result I was looking for:
1 AOUD Roja Dove ENGLAND
2 Amouage for Men Amouage OMAN
3 Angel for Men Thierry Mugler Parfum FRANCE
4 Aqua Vitae Francis Kurkdjian FRANCE
5 Aramis for Men Aramis SWITZERLAND
6 BVLGARI for men BVLGari SWITZERLAND
7 BelAmi Hermis FRANCE
8 Bijan for Men Bijan USA
9 CH for Men Charolina Herrera SPAIN
10 Carnal Flower Frederic Malle FRANCE
11 Chaleur D Animale Chaleur USA
Connor McDonald
January 14, 2017 - 7:47 am UTC

glad we could help

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.