Please explain query intervals output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lenygold via DBMonster.com

    Please explain query intervals output

    I found this query on older thread and i can not uderstand output interval
    pairs:

    How to find min and max values in date intervals:
    --------------------------------------------------
    Input:
    CREATE TABLE INTERVALS
    (key CHAR(5) NOT NULL
    ,level CHAR(7) NOT NULL
    ,date_from DATE NOT NULL
    ,date_to DATE NOT NULL
    );

    INSERT INTO INTERVALS
    VALUES
    ('key1', 'level1', '2001-05-01', '2002-10-01')
    ,('key1', 'level1', '2001-01-01', '2001-04-30')
    ,('key1', 'level1', '2000-11-10', '2000-12-31')
    ,('key1', 'level1', '2000-06-01', '2000-10-09')
    ,('key1', 'level1', '2000-01-01', '2000-05-31')
    ;

    SELECT l.key, l.level, l.date_from, r.date_to
    FROM Intervals l
    Intervals r,
    WHERE NOT EXISTS
    (SELECT *
    FROM Intervals le
    WHERE le.date_to = l.date_from - 1 DAY)
    AND r.date_to =
    (SELECT MIN(date_to)
    FROM Intervals rm
    WHERE rm.date_to l.date_from
    AND NOT EXISTS
    (SELECT *
    FROM Intervals re
    WHERE re.date_from = rm.date_to + 1 DAY));

    or

    SELECT l.key, l.level, l.date_from
    , (SELECT MIN(date_to)
    FROM Intervals rm
    WHERE rm.date_to l.date_from
    AND NOT EXISTS
    (SELECT *
    FROM Intervals re
    WHERE re.date_from = rm.date_to + 1 DAY)
    ) AS date_to
    FROM Intervals l
    WHERE NOT EXISTS
    (SELECT *
    FROM Intervals le
    WHERE le.date_to = l.date_from - 1 DAY);

    Same output:

    KEY LEVEL DATE_FROM DATE_TO
    ----- ------- ---------- ----------
    key1 level1 2000-11-10 2002-10-01
    key1 level1 2000-01-01 2000-10-09

    2 record(s) selected.
    What is the meaning of this output;
    The min value in intervals are
    2001-01-01
    The max value in intervals are
    2002-10-01
    Please help
    Thank's

    --
    Message posted via DBMonster.com


  • Lennart

    #2
    Re: Please explain query intervals output

    On 3 Okt, 14:42, "lenygold via DBMonster.com" <u41482@uwewrot e:
    [...]
    >
    INSERT INTO INTERVALS
    VALUES
     ('key1', 'level1', '2001-05-01', '2002-10-01')
    ,('key1', 'level1', '2001-01-01', '2001-04-30')
    ,('key1', 'level1', '2000-11-10', '2000-12-31')
    ,('key1', 'level1', '2000-06-01', '2000-10-09')
    ,('key1', 'level1', '2000-01-01', '2000-05-31')
    ;
    >
    [...]
    KEY   LEVEL   DATE_FROM  DATE_TO    
    ----- ------- ---------- ----------
    key1  level1  2000-11-10 2002-10-01
    key1  level1  2000-01-01 2000-10-09
    >
     2 record(s) selected.
    What is the meaning of this output;
    The min value in intervals are
    2001-01-01
    The max value in intervals are
    2002-10-01
    Please help
    Thank's
    >
    Not sure what you find confusing, but I think it will become clear to
    you if you draw a timeline from '2000-01-01' to '2002-10-01' and then
    put the intervals in the input ontop of that:

    00-01-01..00-05-31
    00-06-01..00-10-09
    <--
    00-11-10..00-12-31
    01-01-01..

    /Lennart

    Comment

    Working...