0

In TABLE_A there is for each ID a minimum SDT and maximum EDT.

Example: for ID=1000 this will be 1/09/2013 and 3/10/2013.

Example: for ID=1001 this will be 1/07/2013 and 3/08/2013.

The expected result will be all records from TABLE_B where the SDT from TABLE_B falls between those minimum SDT and maximum EDT values from TABLE_A.

Example Fiddle

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

TABLE_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
Community
  • 1
  • 1
user1875323
  • 137
  • 1
  • 2
  • 9

2 Answers2

1

You can use group by for the min/max values and a correlated sub query to relate to 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

Laurence
  • 10,896
  • 1
  • 25
  • 34
0

It's worth noting that using a GROUP BY in the subquery will have unnecessary performance costs.

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
);

Edit/Correction:
This solution would only apply if you were matching against the individual rows in table_a, not a MIN/MAX grouping. I had previously overlooked the MIN/MAX grouping requirement from table_a.

The following uses two EXISTS queries without a GROUP BY and returns the correct results:

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
));
MeyerRJ
  • 792
  • 4
  • 8
  • Ah, I had missed the grouping requirement when I first read this. You are correct sir. :) If it were looking for BETWEEN matches against the individual rows, then my query would have correct. – MeyerRJ Oct 03 '13 at 23:49