Calculating Overlapping Time Intervals in SQL between several users










2















after quite some time spent online to try to find an answer to my problem I decided to post it here.



I have a table which contains intervals of time during which some resources are not available. I would like to extract all time interval in which all resources are not available.



With the example below, I would have: start: 2018-11-16 12:30:00, end: 2018-11-16 12:45:00



Start End Resource
------------------- ------------------- ----------
2018-11-15 12:00:00 2018-11-15 13:00:00 resource A
2018-11-15 12:00:00 2018-11-15 13:00:00 resource B
2018-11-15 12:30:00 2018-11-15 14:00:00 resource C
2018-11-15 12:00:00 2018-11-15 12:45:00 resource D
2018-11-18 12:00:00 2018-11-18 13:00:00 resource A
2018-11-19 11:40:00 2018-11-19 12:20:00 resource B
2018-11-15 16:00:00 2018-11-15 17:00:00 resource D


Would someone have any idea?



Thanks in advance.










share|improve this question
























  • What version of MySQL are you using? This is so cumbersome prior to 8.0 that it may not be worth attempting.

    – Gordon Linoff
    Nov 14 '18 at 20:41











  • Well, actually I use postgresql, the version 9.5.

    – T. Balzer
    Nov 14 '18 at 20:45











  • There is no time interval in your sample data when no resources are available. During 2018-11-16 12:30:00 to 2018-11-16 14:00:00 only resource C is unavailable

    – Joakim Danielson
    Nov 14 '18 at 20:53












  • Made a mistake with my sample. I just corrected it.

    – T. Balzer
    Nov 14 '18 at 21:18






  • 2





    I forgot to ask but how do we now how many "all resources" are or is that dynamic and something that is part of the query and if so do we know all resources will be represented in the table?

    – Joakim Danielson
    Nov 14 '18 at 21:24















2















after quite some time spent online to try to find an answer to my problem I decided to post it here.



I have a table which contains intervals of time during which some resources are not available. I would like to extract all time interval in which all resources are not available.



With the example below, I would have: start: 2018-11-16 12:30:00, end: 2018-11-16 12:45:00



Start End Resource
------------------- ------------------- ----------
2018-11-15 12:00:00 2018-11-15 13:00:00 resource A
2018-11-15 12:00:00 2018-11-15 13:00:00 resource B
2018-11-15 12:30:00 2018-11-15 14:00:00 resource C
2018-11-15 12:00:00 2018-11-15 12:45:00 resource D
2018-11-18 12:00:00 2018-11-18 13:00:00 resource A
2018-11-19 11:40:00 2018-11-19 12:20:00 resource B
2018-11-15 16:00:00 2018-11-15 17:00:00 resource D


Would someone have any idea?



Thanks in advance.










share|improve this question
























  • What version of MySQL are you using? This is so cumbersome prior to 8.0 that it may not be worth attempting.

    – Gordon Linoff
    Nov 14 '18 at 20:41











  • Well, actually I use postgresql, the version 9.5.

    – T. Balzer
    Nov 14 '18 at 20:45











  • There is no time interval in your sample data when no resources are available. During 2018-11-16 12:30:00 to 2018-11-16 14:00:00 only resource C is unavailable

    – Joakim Danielson
    Nov 14 '18 at 20:53












  • Made a mistake with my sample. I just corrected it.

    – T. Balzer
    Nov 14 '18 at 21:18






  • 2





    I forgot to ask but how do we now how many "all resources" are or is that dynamic and something that is part of the query and if so do we know all resources will be represented in the table?

    – Joakim Danielson
    Nov 14 '18 at 21:24













2












2








2


1






after quite some time spent online to try to find an answer to my problem I decided to post it here.



I have a table which contains intervals of time during which some resources are not available. I would like to extract all time interval in which all resources are not available.



With the example below, I would have: start: 2018-11-16 12:30:00, end: 2018-11-16 12:45:00



Start End Resource
------------------- ------------------- ----------
2018-11-15 12:00:00 2018-11-15 13:00:00 resource A
2018-11-15 12:00:00 2018-11-15 13:00:00 resource B
2018-11-15 12:30:00 2018-11-15 14:00:00 resource C
2018-11-15 12:00:00 2018-11-15 12:45:00 resource D
2018-11-18 12:00:00 2018-11-18 13:00:00 resource A
2018-11-19 11:40:00 2018-11-19 12:20:00 resource B
2018-11-15 16:00:00 2018-11-15 17:00:00 resource D


Would someone have any idea?



Thanks in advance.










share|improve this question
















after quite some time spent online to try to find an answer to my problem I decided to post it here.



I have a table which contains intervals of time during which some resources are not available. I would like to extract all time interval in which all resources are not available.



With the example below, I would have: start: 2018-11-16 12:30:00, end: 2018-11-16 12:45:00



Start End Resource
------------------- ------------------- ----------
2018-11-15 12:00:00 2018-11-15 13:00:00 resource A
2018-11-15 12:00:00 2018-11-15 13:00:00 resource B
2018-11-15 12:30:00 2018-11-15 14:00:00 resource C
2018-11-15 12:00:00 2018-11-15 12:45:00 resource D
2018-11-18 12:00:00 2018-11-18 13:00:00 resource A
2018-11-19 11:40:00 2018-11-19 12:20:00 resource B
2018-11-15 16:00:00 2018-11-15 17:00:00 resource D


Would someone have any idea?



Thanks in advance.







sql postgresql postgresql-9.5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 21:18







T. Balzer

















asked Nov 14 '18 at 20:40









T. BalzerT. Balzer

162




162












  • What version of MySQL are you using? This is so cumbersome prior to 8.0 that it may not be worth attempting.

    – Gordon Linoff
    Nov 14 '18 at 20:41











  • Well, actually I use postgresql, the version 9.5.

    – T. Balzer
    Nov 14 '18 at 20:45











  • There is no time interval in your sample data when no resources are available. During 2018-11-16 12:30:00 to 2018-11-16 14:00:00 only resource C is unavailable

    – Joakim Danielson
    Nov 14 '18 at 20:53












  • Made a mistake with my sample. I just corrected it.

    – T. Balzer
    Nov 14 '18 at 21:18






  • 2





    I forgot to ask but how do we now how many "all resources" are or is that dynamic and something that is part of the query and if so do we know all resources will be represented in the table?

    – Joakim Danielson
    Nov 14 '18 at 21:24

















  • What version of MySQL are you using? This is so cumbersome prior to 8.0 that it may not be worth attempting.

    – Gordon Linoff
    Nov 14 '18 at 20:41











  • Well, actually I use postgresql, the version 9.5.

    – T. Balzer
    Nov 14 '18 at 20:45











  • There is no time interval in your sample data when no resources are available. During 2018-11-16 12:30:00 to 2018-11-16 14:00:00 only resource C is unavailable

    – Joakim Danielson
    Nov 14 '18 at 20:53












  • Made a mistake with my sample. I just corrected it.

    – T. Balzer
    Nov 14 '18 at 21:18






  • 2





    I forgot to ask but how do we now how many "all resources" are or is that dynamic and something that is part of the query and if so do we know all resources will be represented in the table?

    – Joakim Danielson
    Nov 14 '18 at 21:24
















What version of MySQL are you using? This is so cumbersome prior to 8.0 that it may not be worth attempting.

– Gordon Linoff
Nov 14 '18 at 20:41





What version of MySQL are you using? This is so cumbersome prior to 8.0 that it may not be worth attempting.

– Gordon Linoff
Nov 14 '18 at 20:41













Well, actually I use postgresql, the version 9.5.

– T. Balzer
Nov 14 '18 at 20:45





Well, actually I use postgresql, the version 9.5.

– T. Balzer
Nov 14 '18 at 20:45













There is no time interval in your sample data when no resources are available. During 2018-11-16 12:30:00 to 2018-11-16 14:00:00 only resource C is unavailable

– Joakim Danielson
Nov 14 '18 at 20:53






There is no time interval in your sample data when no resources are available. During 2018-11-16 12:30:00 to 2018-11-16 14:00:00 only resource C is unavailable

– Joakim Danielson
Nov 14 '18 at 20:53














Made a mistake with my sample. I just corrected it.

– T. Balzer
Nov 14 '18 at 21:18





Made a mistake with my sample. I just corrected it.

– T. Balzer
Nov 14 '18 at 21:18




2




2





I forgot to ask but how do we now how many "all resources" are or is that dynamic and something that is part of the query and if so do we know all resources will be represented in the table?

– Joakim Danielson
Nov 14 '18 at 21:24





I forgot to ask but how do we now how many "all resources" are or is that dynamic and something that is part of the query and if so do we know all resources will be represented in the table?

– Joakim Danielson
Nov 14 '18 at 21:24












1 Answer
1






active

oldest

votes


















0














If you know the number of resources (or it can be calculated) and there's no overlapping Start/End range for a resource (i.e. you got clean data):



select Start, "End"
from
( -- calculate a Cumulative Sum to get the count of resources unavailable at any point in time
select Start,
lead(Start) over (order by Start, x) as "End",
sum(x) over (order by Start, x rows unbounded preceding) as cnt
from
( -- combine starts & ends into a single table and assigne +/-1 to each row
select Start, 1 as x
from tab
union all
select "End", -1 as x
from tab
) as dt
) as dt
where cnt = 4 -- find the row where all resources are unavailable
-- where cnt = (select count(*) from resources)





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308404%2fcalculating-overlapping-time-intervals-in-sql-between-several-users%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you know the number of resources (or it can be calculated) and there's no overlapping Start/End range for a resource (i.e. you got clean data):



    select Start, "End"
    from
    ( -- calculate a Cumulative Sum to get the count of resources unavailable at any point in time
    select Start,
    lead(Start) over (order by Start, x) as "End",
    sum(x) over (order by Start, x rows unbounded preceding) as cnt
    from
    ( -- combine starts & ends into a single table and assigne +/-1 to each row
    select Start, 1 as x
    from tab
    union all
    select "End", -1 as x
    from tab
    ) as dt
    ) as dt
    where cnt = 4 -- find the row where all resources are unavailable
    -- where cnt = (select count(*) from resources)





    share|improve this answer



























      0














      If you know the number of resources (or it can be calculated) and there's no overlapping Start/End range for a resource (i.e. you got clean data):



      select Start, "End"
      from
      ( -- calculate a Cumulative Sum to get the count of resources unavailable at any point in time
      select Start,
      lead(Start) over (order by Start, x) as "End",
      sum(x) over (order by Start, x rows unbounded preceding) as cnt
      from
      ( -- combine starts & ends into a single table and assigne +/-1 to each row
      select Start, 1 as x
      from tab
      union all
      select "End", -1 as x
      from tab
      ) as dt
      ) as dt
      where cnt = 4 -- find the row where all resources are unavailable
      -- where cnt = (select count(*) from resources)





      share|improve this answer

























        0












        0








        0







        If you know the number of resources (or it can be calculated) and there's no overlapping Start/End range for a resource (i.e. you got clean data):



        select Start, "End"
        from
        ( -- calculate a Cumulative Sum to get the count of resources unavailable at any point in time
        select Start,
        lead(Start) over (order by Start, x) as "End",
        sum(x) over (order by Start, x rows unbounded preceding) as cnt
        from
        ( -- combine starts & ends into a single table and assigne +/-1 to each row
        select Start, 1 as x
        from tab
        union all
        select "End", -1 as x
        from tab
        ) as dt
        ) as dt
        where cnt = 4 -- find the row where all resources are unavailable
        -- where cnt = (select count(*) from resources)





        share|improve this answer













        If you know the number of resources (or it can be calculated) and there's no overlapping Start/End range for a resource (i.e. you got clean data):



        select Start, "End"
        from
        ( -- calculate a Cumulative Sum to get the count of resources unavailable at any point in time
        select Start,
        lead(Start) over (order by Start, x) as "End",
        sum(x) over (order by Start, x rows unbounded preceding) as cnt
        from
        ( -- combine starts & ends into a single table and assigne +/-1 to each row
        select Start, 1 as x
        from tab
        union all
        select "End", -1 as x
        from tab
        ) as dt
        ) as dt
        where cnt = 4 -- find the row where all resources are unavailable
        -- where cnt = (select count(*) from resources)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 22:19









        dnoethdnoeth

        45.6k31839




        45.6k31839





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308404%2fcalculating-overlapping-time-intervals-in-sql-between-several-users%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Museum of Modern and Contemporary Art of Trento and Rovereto

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully