I have a DB view which basically consists of two SELECT
queries with UNION ALL
, like this:
CREATE VIEW v AS
SELECT time, etc. FROM t1 /
A possibility would be to issue a new SQL dynamically at each call instead of creating a view and to integrate the where clause in each SELECT of the union query
SELECT time, etc. FROM t1
WHERE time >= ... AND time < ...
UNION ALL
SELECT time, etc. FROM t2
WHERE time >= ... AND time < ...
EDIT:
Can you use a parametrized function?
CREATE OR REPLACE FUNCTION CallMyView(t1 date, t2 date)
RETURNS TABLE(d date, etc.)
AS $$
BEGIN
RETURN QUERY
SELECT time, etc. FROM t1
WHERE time >= t1 AND time < t2
UNION ALL
SELECT time, etc. FROM t2
WHERE time >= t1 AND time < t2;
END;
$$ LANGUAGE plpgsql;
Call
SELECT * FROM CallMyView(..., ...);