有表:
1.current_table:
date value
02.10.2019 1
03.10.2019 2
04.10.2019 2
05.10.2019 -1
06.10.2019 1
07.10.2019 1
08.10.2019 2
09.10.2019 2
10.10.2019 -1
11.10.2019 2
12.10.2019 1
2.intervals
date_start date_end
02.10.2019 04.10.2019 3
06.10.2019 09.10.2019 4
11.10.2019 12.10.2019 2
“间隔”表包含一个不间断的正值序列的最大长度。如何从“间隔”表获取的每个间隔之间选择“当前表”中的行(此类间隔很多)?所以结果应该是:
date value
02.10.2019 1
03.10.2019 2
04.10.2019 2
06.10.2019 1
07.10.2019 1
08.10.2019 2
09.10.2019 2
11.10.2019 2
12.10.2019 1
我的第一个倾向很简单:
select t1.*
from table1 t1
where t1.value > 0;
也许您的间隔可能会重叠。或者,您可能只想过滤第二个表中的间隔。如果是这样,那么exists
很方便:
select t1.*
from table1 t1
where t1.value > 0 and
exists (select 1
from table2 t2
where t1.date between t2.date_start and t2.date_end
);
但是,这对于您的示例数据来说是过大了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句