I've written a solution to problem using PL/SQL and SQL and I can't help thinking that it could be done 100% in SQL but am I am struggling to get started.
Here is the structure of the two tables (If it helps, the scripts to create them are at the end of the question)
Table t1 (primary key is both columns displayed)
ID TYPE
1 A
1 B
1 C
2 A
2 B
3 B
The Type column is a Foreign Key to table T2 which contains the following data:
Table t2 (primary key is Type)
Type Desc
A xx
B xx
C xx
So given the the data in T1 the result I need will be:
For ID 1 because it has all the types in the foreign key table I would return the literal "All"
For ID 2 because it has two types I would like to return "A & B" (note the separator)
And finally for ID 3 because it has one type I would like to return just "B"
As promised here are the scripts to create all the objects mentioned.
create table t2(type varchar2(1),
description varchar2(100)
)
/
insert into t2
values ('A', 'xx')
/
insert into t2
values ('B', 'xx')
/
insert into t2
values ('C', 'xx')
/
alter table t2 add constraint t2_pk primary key (type)
/
create table t1 (id number(10),
type varchar2(1)
)
/
alter table t1 add constraint t1_pk primary key(id, type)
/
alter table t1 add constraint t1_fk foreign key (type)
references t2(type)
/
insert into t1
values (1, 'A')
/
insert into t1
values (1, 'B')
/
insert into t1
values (1, 'C')
/
insert into t1
values (2, 'A')
/
insert into t1
values (2, 'B')
/
insert into t1
values (3, 'B')
/
You can use SQL trace to capture execution stats for a procedure. But this only gathers SQL execution details. If you want to know details of the PL/SQL execution, you can use the hierarchical profiler.
PL/SQL enables you to mix SQL statements with procedural constructs. With PL/SQL, you can create and run PL/SQL program units such as procedures, functions, and packages. PL/SQL program units generally are categorized as anonymous blocks, stored functions, stored procedures, and packages.
In SQL Developer, you don't have to use EXPLAIN PLAN FOR statement. Press F10 or click the Explain Plan icon. It will be then displayed in the Explain Plan window. If you are using SQL*Plus then use DBMS_XPLAN.
With PL/SQL, it is very simple to issue a query, retrieve each row of the result into a %ROWTYPE record, and process each row in a loop: You include the text of the query directly in the FOR loop. PL/SQL creates a record variable with fields corresponding to the columns of the result set.
Something like this should get you what you are looking for:
select
id,
case
when cnt = (select count(distinct type) from t2)
then 'All'
else ltrim(sys_connect_by_path(type,' & '),' &')
end types
from (
select
t1.id,
t2.type,
count(*) over (partition by t1.id) cnt,
row_number() over (partition by t1.id order by t2.type) rn
from
t1
inner join t2
on t2.type = t1.type
)
where
rn = cnt
start with rn = 1
connect by prior id = id and prior rn = rn-1;
I would give your question +10 if I could for posting your object / data creation script!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With