Trying to compare date fields

  • I have been on a 3-year hiatus from SQL and I'm just coming back to it, trying to refresh my memory.  I have a basic querying problem with two tables I have as follows:

    Table1: date1, identifier1, data1

    Table2: date2, identifier2, data2

    The identifiers from these tables match exactly, which is perfect for my inner join.  However, the dates do not.  I would like to return all data from Table1 for which date1 falls in between successive dates of date2 for a given identifier.

    I know I have to use a nested query for this, but I don't know how to go about doing it.  Can anybody write back with how one would structure such a subquery?

    Any help much appreciated!

    -Paul

  • maybe this is a start ...

    select t1.date1, identifier1, data1

    from table1 t1

    where t1.date1 between (select min(date2)

                                         from table2 t2min

                                            where t2min.identifier2 = t1.identifier1

                                               and t2min.date2 > t1.date1)

                          and (select max(date2)

                                         from table2 t2max

                                            where t2max.identifier2 = t1.identifier1

                                               and t2max.date2 < t1.date1)

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply