Solved

SQL* Plus: Select rownums for each unique transaction

Posted on 2007-11-23
2
299 Views
Last Modified: 2013-12-19
I have the table below in SQL*Plus: 8.0.6.0.0

drop table my_data;

create table my_data
(transaction                         NUMBER(6),
 my_product                        VARCHAR2(6),
 my_product_colour              VARCHAR2(35)
);

insert into my_data
values('12345','BAG','BLACK');
insert into my_data
values('12345','BAG','BLUE');
insert into my_data
values('12345','BAG','BLACK');
insert into my_data
values('12345','SHOES','RED');
insert into my_data
values('15896','BAG','BLACK');
insert into my_data
values('17845','BAG','BLACK');
insert into my_data
values('17845','SHOES','RED');

select * from my_data;

TRANSACTION MY_PRO   COLOUR
----------- ------ -----------------------------------
      12345       BAG         BLACK
      12345       BAG          BLUE
      12345       BAG          BLACK
      12345       SHOES         RED
      15896       BAG          BLACK
      17845       BAG          BLACK
      17845       SHOES          RED

7 rows selected.

What I want to do is select the data above but also with the line number
eg first product in the transaction to be number 1, then 2 etc,
then next transaction 1, 2 etc like below.

TRANSACTION MY_PRO COLOUR            my_row_nums      
----------- ------ -----------------------------------
      12345       BAG          BLACK                1
      12345       BAG          BLUE                                      2
      12345       BAG          BLACK                                    3
      12345       BAG          BLACK                                   4
      15896       BAG          BLACK            1
      17845       BAG         BLACK                                   1
      17845       SHOES          RED                                    2

Thanks,

Jason.
0
Comment
Question by:JasonAsh
2 Comments
 
LVL 27

Accepted Solution

by:
sujith80 earned 50 total points
Comment Utility
select TRANSACTION , MY_PROduct , my_product_COLOUR , row_number() over( partition by transaction order by MY_PROduct ) my_row_nums      
from my_data
/
0
 

Author Comment

by:JasonAsh
Comment Utility
Thanks Sujith80 that works perfect.

Thanks for the quick response,

Jason.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

771 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

7 Experts available now in Live!

Get 1:1 Help Now