根据另一个查询返回的条件在 postgres 中触发不同的查询
firing different query in postgres based on condition returned by another query
根据一个 sql 查询返回的结果,我必须决定调用哪个 SQL 查询。可能吗?
以下为演示:
select start, end, max from table.
If max < 10
select ob1, ob2, start, end from t1
if max >=10 and < 50
select ob1, ob2, start, end from t2
if max >= 50
select ob1, ob2, start, end from t2
您可以使用类似 条件联合:
select ob1, ob2, q2."start", q2."end"
from (
select "start", "end", "max"
from the_table
) q1,
lateral (
select ob1, ob2, "start", "end"
from t1
where "max" < 10
union
select ob1, ob2, "start", "end"
from t2
where "max" >=10 and "max" < 50
union
select ob1, ob2, "start", "end"
from t3
where "max" >= 50
) q2
阅读文档中的 LATERAL Subqueries。
在 Postgres 9.2 中可以使用 with
查询:
with m as (
select "max"
from the_table)
select ob1, ob2, q."start", q."end"
from (
select ob1, ob2, "start", "end"
from t1, m
where "max" < 10
union
select ob1, ob2, "start", "end"
from t2, m
where "max" >=10 and "max" < 50
union
select ob1, ob2, "start", "end"
from t3, m
where "max" >= 50
) q
CREATE OR replace FUNCTION fnn ()
RETURNS setof t1 AS $$
DECLARE sql TEXT;
BEGIN
SELECT CASE
WHEN max < 10
THEN 'select ob1, ob2, istart, iend from t1'
WHEN max >= 50
THEN ' select ob1, ob2, istart, iend from t2;'
WHEN max > 10
THEN ' select ob1, ob2, istart, iend from t3;'
END AS qry
INTO sql
FROM itable;
RETURN QUERY
EXECUTE (sql);
END $$
LANGUAGE plpgsql
用法:
select * from fnn()
或
DO $$
DECLARE sql TEXT;
BEGIN
SELECT CASE
WHEN max < 10
THEN 'select ob1, ob2, istart, iend from t1'
WHEN max >= 50
THEN ' select ob1, ob2, istart, iend from t2;'
WHEN max > 10
THEN ' select ob1, ob2, istart, iend from t3;'
END AS qry
INTO sql
FROM itable;
DROP TABLE IF EXISTS t;
EXECUTE ('create temp table t as ' || sql);
END $$;
select * from t;
根据一个 sql 查询返回的结果,我必须决定调用哪个 SQL 查询。可能吗?
以下为演示:
select start, end, max from table.
If max < 10
select ob1, ob2, start, end from t1
if max >=10 and < 50
select ob1, ob2, start, end from t2
if max >= 50
select ob1, ob2, start, end from t2
您可以使用类似 条件联合:
select ob1, ob2, q2."start", q2."end"
from (
select "start", "end", "max"
from the_table
) q1,
lateral (
select ob1, ob2, "start", "end"
from t1
where "max" < 10
union
select ob1, ob2, "start", "end"
from t2
where "max" >=10 and "max" < 50
union
select ob1, ob2, "start", "end"
from t3
where "max" >= 50
) q2
阅读文档中的 LATERAL Subqueries。
在 Postgres 9.2 中可以使用 with
查询:
with m as (
select "max"
from the_table)
select ob1, ob2, q."start", q."end"
from (
select ob1, ob2, "start", "end"
from t1, m
where "max" < 10
union
select ob1, ob2, "start", "end"
from t2, m
where "max" >=10 and "max" < 50
union
select ob1, ob2, "start", "end"
from t3, m
where "max" >= 50
) q
CREATE OR replace FUNCTION fnn ()
RETURNS setof t1 AS $$
DECLARE sql TEXT;
BEGIN
SELECT CASE
WHEN max < 10
THEN 'select ob1, ob2, istart, iend from t1'
WHEN max >= 50
THEN ' select ob1, ob2, istart, iend from t2;'
WHEN max > 10
THEN ' select ob1, ob2, istart, iend from t3;'
END AS qry
INTO sql
FROM itable;
RETURN QUERY
EXECUTE (sql);
END $$
LANGUAGE plpgsql
用法:
select * from fnn()
或
DO $$
DECLARE sql TEXT;
BEGIN
SELECT CASE
WHEN max < 10
THEN 'select ob1, ob2, istart, iend from t1'
WHEN max >= 50
THEN ' select ob1, ob2, istart, iend from t2;'
WHEN max > 10
THEN ' select ob1, ob2, istart, iend from t3;'
END AS qry
INTO sql
FROM itable;
DROP TABLE IF EXISTS t;
EXECUTE ('create temp table t as ' || sql);
END $$;
select * from t;