• luboxing (8/30/2014)


    Thanks Tom. Not exactly -- I need dates along with missing locations on those dates.

    well, a first step towards that is

    SELECT A.LOCATN_K, A.STRT_D, A,XXCHG_OUT_D FROM TBL1 as A WHERE not exists (

    select 1 from TBL2 as C where

    C.LOCATN_K = A.LOCATN_K and

    A.STRT_D < C.DATE and

    (A.XCHG_OUT_D > C.DATE OR A.XCHG_OUT_D IS NULL)

    ) ;

    Using that as a CTE or a subquery a join with a tally table (or a tally CTE) can be used to blow up each row into a row for each day its range covers, with the results sorted by date and location code; if you then want to denormalise the result of that to get rows each with a date and a list of location codes that's probably best left to formatting outside the SQL system, although it can be done in the query if necessary.

    edit: doing that of course requires replacing NULL XCHG_OUT dates by a specific end date, which could be either handed in as a constant or calculated in the query, for example as the latest of any date mentioned in any of the three date columns accessed by the query, or the day the query is run on if that is later.

    Tom