内联视图、标量子查询、WITH子查询分解的示例
-- 内联视图 ( 子查询在from后面,作为一个表 )
select p.pname,c1_sum1,c2_sum2
from p,
(select id,sum(q1) c1_sum1 from s1 group by id) s1,
(select id,sum(q2) c2_sum2 from s2 group by id) s2
where p.id=s1.id
and p.id=s2.id
-- 标量子查询(子查询在select后面,作为一个字段,可与表关联)
select
p.pname,
(select sum(q1) c1_sum1 from s1 where s1.id=p.id) c1_sum1,
(select sum(q2) c1_sum1 from s2 where s2.id=p.id) c2_sum2
from p
-- with子查询分解( 先 定义子查询作为一个变量,再使用)
with c1_vw as (select id,sum(q1) sum1 from s1 group by id),
c2_vw as (select id,sum(q2) sum2 from s2 group by id),
c1_c2 as (select t1.id,t1.sum1,t2.sum2 from c1_vw t1,c2_vw t2 where t1.id=t2.id)
select p.pname,sum1,sum2
from p,c1_c2
where p.id=c1_c2.id
-- 内联视图 ( 子查询在from后面,作为一个表 )
select p.pname,c1_sum1,c2_sum2
from p,
(select id,sum(q1) c1_sum1 from s1 group by id) s1,
(select id,sum(q2) c2_sum2 from s2 group by id) s2
where p.id=s1.id
and p.id=s2.id
-- 标量子查询(子查询在select后面,作为一个字段,可与表关联)
select
p.pname,
(select sum(q1) c1_sum1 from s1 where s1.id=p.id) c1_sum1,
(select sum(q2) c1_sum1 from s2 where s2.id=p.id) c2_sum2
from p
-- with子查询分解( 先 定义子查询作为一个变量,再使用)
with c1_vw as (select id,sum(q1) sum1 from s1 group by id),
c2_vw as (select id,sum(q2) sum2 from s2 group by id),
c1_c2 as (select t1.id,t1.sum1,t2.sum2 from c1_vw t1,c2_vw t2 where t1.id=t2.id)
select p.pname,sum1,sum2
from p,c1_c2
where p.id=c1_c2.id