I have a function which returns min_time
and max_time
which I would like to use in a SELECT query as constraints for a timestamp column. Below is how I can think to do it but I would prefer to not have to run the query twice.
SELECT timestamp, col FROM data
WHERE timestamp BETWEEN
(SELECT min_time FROM get_time_limits())
AND
(SELECT max_time FROM get_time_limits());
Is there a way to use multiple columns from a single nested SELECT query as constraints in another query?
Sample table:
CREATE TABLE test(timestamp TIMESTAMPTZ, col INTEGER);
INSERT INTO test (
SELECT generate_series(
NOW() - INTERVAL '10 days',
NOW(),
INTERVAL '1 day'),
generate_series(1, 11)
);
Example min/max time:
'2023-03-23 00:00:00+00'::TIMESTAMPTZ
'2023-03-29 00:00:00+00'::TIMESTAMPTZ
get_time_limits()
returnsmin_time::TIMESTAMPTZ, max_time::TIMESTAMPTZ
. The table,data
, has columnstimestamp::TIMESTAMPTZ, col INTEGER
.get_time_limits()
a STABLE function? ({ IMMUTABLE | STABLE | VOLATILE }
)