我根据下表中的两列编写了一份相当简单的报告 如果is_sender为true,则在报告中添加一行“sender” 如果两者都打开,则将两条记录添加到报告中 因此,对于以下数据,结果如下:
sender, Robert
receiver,Robert
receiver,Marco
我使用“with子句”和结果联合实现了所需的报告,但其性能非常糟糕,
有没有办法使用分析功能或更好的方法来做同样的事情?
create table users_table(
name varchar2(10),
is_sender char(1),
is_receiver char(1)
)
insert into users_table values('Robert','y','n');
insert into users_table values('Marco','y','y');
create or replace view users_view ("role", "name") as
(
select role,name from (
with sender_reciver_view as (select name,is_sender,is_receiver from users_table)
select 'sender' as role,name from sender_reciver_view where is_sender='y'
union all
select 'receiver',name from sender_reciver_view where is_receiver='y')
)
答案 0 :(得分:1)
我假设在你的例子中,Marco应该有两行,对吗?
select
m.role,
ut.name
from users_table ut
join (
select 'sender' as role from dual union all
select 'receiver' from dual
) m
on (m.role = 'sender' and ut.is_sender = 'y')
or (m.role = 'receiver' and ut.is_receiver = 'y');
结果:
ROLE NAME
sender Robert
sender Marco
receiver Marco