在TABLE_A中,每个ID都有一个最小SDT和最大EDT。
示例:对于ID = 1000,这将是1/09/2013和3/10/2013。
示例:对于ID = 1001,这将是1/07/2013和3/08/2013。
预期结果将是来自TABLE_B的所有记录,其中来自TABLE_B的SDT落在来自TABLE_A的那些最小SDT和最大EDT值之间。
TABLE_A
ID SDT EDT
------------------------------
1000 1/10/2013 3/10/2013
1000 1/09/2013 3/09/2013
1001 1/08/2013 3/08/2013
1001 1/07/2013 3/07/2013
1002 1/06/2013 3/06/2013
表-B
ID SDT
-----------------
1000 2/10/2013 ===> because it falls between 1/09/2013 and 3/10/2013
1000 4/09/2013 ===> because it falls between 1/09/2013 and 3/10/2013
1001 2/08/2013 ===> because it falls between 1/07/2013 and 3/08/2013
1001 4/05/2013 ==> not in result because it's not between 1/07/2013 and 3/08/2013
1002 4/06/2013 ==> also out of range, so not in results
答案 0 :(得分:1)
您可以将group by用于最小值/最大值,将相关子查询用于table_b
Select
*
From
Table_b b
Where
Exists (
Select
'x'
From
table_a a
Where
a.id = b.id
group by
a.id
having
min(a.sdt) <= b.sdt and
max(a.edt) >= b.sdt
)
<强> Example Fiddle 强>
答案 1 :(得分:0)
值得注意的是,在子查询中使用GROUP BY会产生不必要的性能成本。
SELECT b.*
FROM table_b b
WHERE EXISTS (
SELECT 1
FROM table_a a
WHERE b.sdt BETWEEN LEAST(a.sdt, e.edt) AND GREATEST(a.edt, a.sdt)
AND a.id = b.id
);
编辑/校正:
只有在匹配table_a中的各个行而不是MIN / MAX分组时,此解决方案才适用。我以前忽略了table_a的MIN / MAX分组要求。
以下使用两个没有GROUP BY的EXISTS查询并返回正确的结果:
SELECT b.*
FROM table_b b
WHERE (EXISTS (
SELECT 1
FROM table_a a
WHERE b.sdt >= a.sdt
AND a.id = b.id
) AND EXISTS (
SELECT 1
FROM table_a a
WHERE b.sdt <= a.edt
AND a.id = b.id
));