Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to build the SQL statement

LudyNov 16 2009 — edited Nov 16 2009
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
This post has been answered by Frank Kulash on Nov 16 2009
Jump to Answer

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 14 2009
Added on Nov 16 2009
2 comments
515 views