0% found this document useful (0 votes)
55 views

Example Oracle PL/SQL With Partition by

This document outlines how to use the partition by function in PL/SQL to generate a row number for grouped data. It creates a table to store item numbers, inserts sample data, and uses a SELECT statement with the ROW_NUMBER function to assign a sequential number to rows within partitions defined by the item number prefix.

Uploaded by

tracejm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Example Oracle PL/SQL With Partition by

This document outlines how to use the partition by function in PL/SQL to generate a row number for grouped data. It creates a table to store item numbers, inserts sample data, and uses a SELECT statement with the ROW_NUMBER function to assign a sequential number to rows within partitions defined by the item number prefix.

Uploaded by

tracejm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

/* How to Generate a Row Number by Grouping - PL/SQL */

/* Outlines how to use the partition by function with ROWNUM */


CREATE TABLE ITEM_LIST
(RECORD_ID NUMBER GENERATED ALWAYS AS IDENTITY
,ITEM_NUMBER VARCHAR2(100) NOT NULL);

INSERT INTO ITEM_LIST (ITEM_NUMBER) VALUES ('80200');


INSERT INTO ITEM_LIST (ITEM_NUMBER) VALUES ('80300');
INSERT INTO ITEM_LIST (ITEM_NUMBER) VALUES ('80400');
INSERT INTO ITEM_LIST (ITEM_NUMBER) VALUES ('80500');
INSERT INTO ITEM_LIST (ITEM_NUMBER) VALUES ('90200');
INSERT INTO ITEM_LIST (ITEM_NUMBER) VALUES ('90300');
INSERT INTO ITEM_LIST (ITEM_NUMBER) VALUES ('91300');
INSERT INTO ITEM_LIST (ITEM_NUMBER) VALUES ('91400');
/
SELECT RECORD_ID
,ITEM_NUMBER
,SUBSTR(ITEM_NUMBER,1,2) ITEM_PREFIX
,ROW_NUMBER() OVER (PARTITION BY SUBSTR(ITEM_NUMBER,1,2) ORDER BY
SUBSTR(ITEM_NUMBER,1,2)) ROW_WITHIN_PREFIX
FROM ITEM_LIST
/
/* RESULT:
RECORD_ID ITEM_NUMBER ITEM PREFIX ROW_WITHIN_PREFIX
---------- ------------- ----------- -----------------
1 80200 80 1
2 80300 80 2
3 80400 80 3
4 80500 80 4
5 90200 90 1
6 90300 90 2
7 91300 91 1
8 91400 91 2
*/

You might also like