Answer the question
In order to leave comments, you need to log in
Postgresql, how to get rows of a table that fall within intervals recorded in a table with intervals?
i have table t. the values in the t.sequence column are ordered and unique.
there is a table t_intervals with columns id - interval id, start and end - which point to t1.sequence.
i can get data from t for 1 interval like this:
WITH interval AS(
SELECT start, end from t_intervals
WHERE id=10
)
SELECT * FROM t
WHERE sequence BETWEEN (select start from interval)
AND (select end FROM interval);
WITH interval1 AS(
SELECT start, end from t_intervals
WHERE id=10
),
interval2 AS(
SELECT start, end from t_intervals
WHERE id=20
)
SELECT * FROM t WHERE
WHERE sequence BETWEEN (select start from interval1)
AND (select end FROM interval1)
OR sequence BETWEEN (select start from interval2)
AND (select end FROM interval2);
WITH intervals AS(
SELECT start, end from t_intervals
WHERE id in (2,10,30, ..., n)
)
SELECT * FROM t
WHERE OR_ROWS(
sequence BETWEEN (select start from intervals)
AND (select end FROM intervals)
);
sequence BETWEEN (select start from intervals) AND (select end FROM intervals)
Answer the question
In order to leave comments, you need to log in
A complete feeling that something like
SELECT [DISTINCT] t.*
FROM t
JOIN t_intervals ti ON t.sequence BETWEEN ti.start AND ti.end
WHERE ti.id in (2,10,30, ..., n)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question