我有一个案例,我需要在每个函数变量的连接中运行不同的查询
目前我的功能为:
CREATE OR REPLACE FUNCTION a(type_f boolean)
RETURNS SETOF rowtypes AS
$$
declare row rowtypes ;
begin
....
select...
from..
left join (select pp.a, avg(ppp.priceusd) as avgpricetf
from pp
join p on (p.b=pp.b)
join (...) as ppp on (pp.c=p.c)
group by pp.a) tfquery on (tfquery.a=main.a)
....
end;
$$
LANGUAGE plpgsql VOLATILE
这很好用。
我想修改它,以便type_f=True
此查询将在原始查询的连接instad中运行:
left join (select pp.a,p.d, avg(ppp.priceusd) as avgpricetf
from pp
join p on (p.b=pp.b)
join (...) as ppp on (pp.c=p.c)
group by pp.a,p.d) tfquery on (tfquery.a=main.a and tfquery.d=main.d)
正如您所看到的那样,查询的更改以及连接本身的条件也是如此。
Basicly :
type_f=False
时:
left join (select pp.a, avg(ppp.priceusd) as avgpricetf
from pp
join p on (p.b=pp.b)
join (...) as ppp on (pp.c=p.c)
group by pp.a) tfquery on (tfquery.a=main.a)
当:
type_f=True
做:
left join (select pp.a,p.d, avg(ppp.priceusd) as avgpricetf
from pp
join p on (p.b=pp.b)
join (...) as ppp on (pp.c=p.c)
group by pp.a,p.d) tfquery on (tfquery.a=main.a and tfquery.d=main.d)
我该怎么做?
答案 0 :(得分:0)
您可以使用这样的逻辑来消除查询的所有可读性并获得预期结果:
Select *
from A
left outer join
( select * from B where type_f
union all
select *, A.field from C where not type_f
) X
on X.n = A.n and X.field = A.field
....