Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.4K Development
- 17 Developer Projects
- 139 Programming Languages
- 293.1K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 161 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 474 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
How to build the SQL statement

Ludy
Member Posts: 50
Hi,
Below is the table and data, and i had done the solution using a cursor in pl/sql,
can this be accomplished in a SQL statemnt ?
create table TEMP_DISTINCT
(
A_ID NUMBER not null,
B_ID NUMBER not null,
C_VAL VARCHAR2(30)
)
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(111,2222,'One')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(111,2222,'Two')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(112,2222,'Three')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(112,2222,'Four')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(112,2221,'Five')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(112,2221,'Six')
declare
vorgid varchar2(30);
cursor cur is
select distinct A_ID, B_ID from TEMP_DISTINCT
order by A_ID,B_ID;
begin
for curs in cur
loop
select C_VAL
into vorgid from TEMP_DISTINCT
where curs.A_ID = A_ID
and curs.B_ID=B_ID
and rownum = 1;
dbms_output.put_line(curs.A_ID|| ' : ' || curs.B_ID || ':' || substr(vorgid,1,22));
end loop;
end;
Output
-------------
111 : 2222:One
112 : 2221:Five
112 : 2222:Three
Below is the table and data, and i had done the solution using a cursor in pl/sql,
can this be accomplished in a SQL statemnt ?
create table TEMP_DISTINCT
(
A_ID NUMBER not null,
B_ID NUMBER not null,
C_VAL VARCHAR2(30)
)
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(111,2222,'One')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(111,2222,'Two')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(112,2222,'Three')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(112,2222,'Four')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(112,2221,'Five')
INSERT INTO TEMP_DISTINCT(A_ID,B_ID,C_VAL) VALUES(112,2221,'Six')
declare
vorgid varchar2(30);
cursor cur is
select distinct A_ID, B_ID from TEMP_DISTINCT
order by A_ID,B_ID;
begin
for curs in cur
loop
select C_VAL
into vorgid from TEMP_DISTINCT
where curs.A_ID = A_ID
and curs.B_ID=B_ID
and rownum = 1;
dbms_output.put_line(curs.A_ID|| ' : ' || curs.B_ID || ':' || substr(vorgid,1,22));
end loop;
end;
Output
-------------
111 : 2222:One
112 : 2221:Five
112 : 2222:Three
Best Answer
-
Hi,
When you use ROWNUM directly on a table, or on a view or result set that does not have an ORDER BY clause, the results are indeterminate, that is, there is no guaranteee you will get the same results every time you run the query, even if the table hasn't changed.
The analytic ROW_NUMBER function can be used in place of ROWNUM. ROW_NUMBER can compute several different sequences (on different partitions of the result set) at the same time, like this:WITH got_rnum AS ( SELECT a_id , b_id , c_val , ROW_NUMBER () OVER (PARTITION BY a_id, b_id ORDER BY ROWID ) AS rnum FROM temp_distinct ) SELECT a_id , b_id , c_val FROM got_rnum WHERE rnum = 1 ;
When you use ROW_NUMBER, you have to ORDER BY something. In a very small table, like the sample one, the arbitrary results might be coming in ROWID order in your PL/SQL code, so I used "ORDER BY ROWID" above. I could have used a constant, e.g. "ORDER BY 0".
Answers
-
Hi,
When you use ROWNUM directly on a table, or on a view or result set that does not have an ORDER BY clause, the results are indeterminate, that is, there is no guaranteee you will get the same results every time you run the query, even if the table hasn't changed.
The analytic ROW_NUMBER function can be used in place of ROWNUM. ROW_NUMBER can compute several different sequences (on different partitions of the result set) at the same time, like this:WITH got_rnum AS ( SELECT a_id , b_id , c_val , ROW_NUMBER () OVER (PARTITION BY a_id, b_id ORDER BY ROWID ) AS rnum FROM temp_distinct ) SELECT a_id , b_id , c_val FROM got_rnum WHERE rnum = 1 ;
When you use ROW_NUMBER, you have to ORDER BY something. In a very small table, like the sample one, the arbitrary results might be coming in ROWID order in your PL/SQL code, so I used "ORDER BY ROWID" above. I could have used a constant, e.g. "ORDER BY 0". -
Thanks Frank Kulash,
your answer helps me in having a better understanding now.
As i was thinking to use :
select * from temp_distinct where rowid in (select min(rowid) from temp_distinct group by a_id, b_id)
but as you have pointed out rowid arbitrary results might be coming.
Will use the Analytical fn.
This discussion has been closed.