Solved

SQL* Plus: Select rownums for each unique transaction

Posted on 2007-11-23
2
311 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
ID: 20338340
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
ID: 20338372
Thanks Sujith80 that works perfect.

Thanks for the quick response,

Jason.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

713 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