PostgreSQL time difference in minutes from time column?









up vote
1
down vote

favorite












I want to calculate time duration in minutes using PostgreSQL (phone calls).



-- Difference between 08:54:55 and 08:56:10 (time only)



SELECT DATE_PART('hour', '08:56:10'::time - '08:54:55'::time) * 60 +
DATE_PART('minute', '08:56:10'::time - '08:54:55'::time);
-- Result: 1



How can I modify this to work for the whole dataset? Start time (only hour and minute, seconds are always 00) and end time are in the same column Event_Time, grouped by Correlation_ID.



Event_Time Customer_ID Case_ID Event_ID Correlation_ID Line_ID Internal_ID
10:11:00 id1 1 start sd34 l3 456
10:13:00 id5 8 stop rt56 l3 456
10:15:00 id4 3 start tg84 l7 567
10:17:00 id7 5 start ty69 l4 678
10:21:00 id5 5 stop rt56 l7 678
10:31:00 id1 1 stop sd34 l4 567


I tried this solution, but it works only when I specify time http://www.sqlines.com/postgresql/how-to/datediff.



Thanks!










share|improve this question























  • please add more details to your question. Read this for tips on how to ask better sql questions: meta.stackoverflow.com/questions/271055/…
    – Haleemur Ali
    Nov 11 at 19:43










  • Thanks! Just added more info to my question.
    – Iamadriana
    Nov 11 at 20:04










  • I'm assuming the column "Event_Time" is a time column (not some sort of string)
    – Jasen
    Nov 12 at 4:41














up vote
1
down vote

favorite












I want to calculate time duration in minutes using PostgreSQL (phone calls).



-- Difference between 08:54:55 and 08:56:10 (time only)



SELECT DATE_PART('hour', '08:56:10'::time - '08:54:55'::time) * 60 +
DATE_PART('minute', '08:56:10'::time - '08:54:55'::time);
-- Result: 1



How can I modify this to work for the whole dataset? Start time (only hour and minute, seconds are always 00) and end time are in the same column Event_Time, grouped by Correlation_ID.



Event_Time Customer_ID Case_ID Event_ID Correlation_ID Line_ID Internal_ID
10:11:00 id1 1 start sd34 l3 456
10:13:00 id5 8 stop rt56 l3 456
10:15:00 id4 3 start tg84 l7 567
10:17:00 id7 5 start ty69 l4 678
10:21:00 id5 5 stop rt56 l7 678
10:31:00 id1 1 stop sd34 l4 567


I tried this solution, but it works only when I specify time http://www.sqlines.com/postgresql/how-to/datediff.



Thanks!










share|improve this question























  • please add more details to your question. Read this for tips on how to ask better sql questions: meta.stackoverflow.com/questions/271055/…
    – Haleemur Ali
    Nov 11 at 19:43










  • Thanks! Just added more info to my question.
    – Iamadriana
    Nov 11 at 20:04










  • I'm assuming the column "Event_Time" is a time column (not some sort of string)
    – Jasen
    Nov 12 at 4:41












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to calculate time duration in minutes using PostgreSQL (phone calls).



-- Difference between 08:54:55 and 08:56:10 (time only)



SELECT DATE_PART('hour', '08:56:10'::time - '08:54:55'::time) * 60 +
DATE_PART('minute', '08:56:10'::time - '08:54:55'::time);
-- Result: 1



How can I modify this to work for the whole dataset? Start time (only hour and minute, seconds are always 00) and end time are in the same column Event_Time, grouped by Correlation_ID.



Event_Time Customer_ID Case_ID Event_ID Correlation_ID Line_ID Internal_ID
10:11:00 id1 1 start sd34 l3 456
10:13:00 id5 8 stop rt56 l3 456
10:15:00 id4 3 start tg84 l7 567
10:17:00 id7 5 start ty69 l4 678
10:21:00 id5 5 stop rt56 l7 678
10:31:00 id1 1 stop sd34 l4 567


I tried this solution, but it works only when I specify time http://www.sqlines.com/postgresql/how-to/datediff.



Thanks!










share|improve this question















I want to calculate time duration in minutes using PostgreSQL (phone calls).



-- Difference between 08:54:55 and 08:56:10 (time only)



SELECT DATE_PART('hour', '08:56:10'::time - '08:54:55'::time) * 60 +
DATE_PART('minute', '08:56:10'::time - '08:54:55'::time);
-- Result: 1



How can I modify this to work for the whole dataset? Start time (only hour and minute, seconds are always 00) and end time are in the same column Event_Time, grouped by Correlation_ID.



Event_Time Customer_ID Case_ID Event_ID Correlation_ID Line_ID Internal_ID
10:11:00 id1 1 start sd34 l3 456
10:13:00 id5 8 stop rt56 l3 456
10:15:00 id4 3 start tg84 l7 567
10:17:00 id7 5 start ty69 l4 678
10:21:00 id5 5 stop rt56 l7 678
10:31:00 id1 1 stop sd34 l4 567


I tried this solution, but it works only when I specify time http://www.sqlines.com/postgresql/how-to/datediff.



Thanks!







postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 20:03

























asked Nov 11 at 19:39









Iamadriana

64




64











  • please add more details to your question. Read this for tips on how to ask better sql questions: meta.stackoverflow.com/questions/271055/…
    – Haleemur Ali
    Nov 11 at 19:43










  • Thanks! Just added more info to my question.
    – Iamadriana
    Nov 11 at 20:04










  • I'm assuming the column "Event_Time" is a time column (not some sort of string)
    – Jasen
    Nov 12 at 4:41
















  • please add more details to your question. Read this for tips on how to ask better sql questions: meta.stackoverflow.com/questions/271055/…
    – Haleemur Ali
    Nov 11 at 19:43










  • Thanks! Just added more info to my question.
    – Iamadriana
    Nov 11 at 20:04










  • I'm assuming the column "Event_Time" is a time column (not some sort of string)
    – Jasen
    Nov 12 at 4:41















please add more details to your question. Read this for tips on how to ask better sql questions: meta.stackoverflow.com/questions/271055/…
– Haleemur Ali
Nov 11 at 19:43




please add more details to your question. Read this for tips on how to ask better sql questions: meta.stackoverflow.com/questions/271055/…
– Haleemur Ali
Nov 11 at 19:43












Thanks! Just added more info to my question.
– Iamadriana
Nov 11 at 20:04




Thanks! Just added more info to my question.
– Iamadriana
Nov 11 at 20:04












I'm assuming the column "Event_Time" is a time column (not some sort of string)
– Jasen
Nov 12 at 4:41




I'm assuming the column "Event_Time" is a time column (not some sort of string)
– Jasen
Nov 12 at 4:41












1 Answer
1






active

oldest

votes

















up vote
1
down vote













perhaps with a self join like this:



SELECT
start."Correlation_ID",
extract(epoch from stop."Event_time" - start."Event_time")/60
FROM
some_table AS start
JOIN
some_table AS stop
ON start."Correlation_ID"=finish."Correlation_ID"
AND stop."Event_ID"='stop'
WHERE
start."Event_ID"='start'
;


or with a group like this:



SELECT
"Correlation_ID",
extract(epoch from
max(CASE "Event_ID" WHEN 'stop' THEN "Event_time" END )
-min(CASE "Event_ID" WHEN 'start' THEN "Event_time" END )
) /60
FROM
some_table
JOIN
grouP BY "Correlation_ID"
;





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',
    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%2f53252483%2fpostgresql-time-difference-in-minutes-from-time-column%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








    up vote
    1
    down vote













    perhaps with a self join like this:



    SELECT
    start."Correlation_ID",
    extract(epoch from stop."Event_time" - start."Event_time")/60
    FROM
    some_table AS start
    JOIN
    some_table AS stop
    ON start."Correlation_ID"=finish."Correlation_ID"
    AND stop."Event_ID"='stop'
    WHERE
    start."Event_ID"='start'
    ;


    or with a group like this:



    SELECT
    "Correlation_ID",
    extract(epoch from
    max(CASE "Event_ID" WHEN 'stop' THEN "Event_time" END )
    -min(CASE "Event_ID" WHEN 'start' THEN "Event_time" END )
    ) /60
    FROM
    some_table
    JOIN
    grouP BY "Correlation_ID"
    ;





    share|improve this answer


























      up vote
      1
      down vote













      perhaps with a self join like this:



      SELECT
      start."Correlation_ID",
      extract(epoch from stop."Event_time" - start."Event_time")/60
      FROM
      some_table AS start
      JOIN
      some_table AS stop
      ON start."Correlation_ID"=finish."Correlation_ID"
      AND stop."Event_ID"='stop'
      WHERE
      start."Event_ID"='start'
      ;


      or with a group like this:



      SELECT
      "Correlation_ID",
      extract(epoch from
      max(CASE "Event_ID" WHEN 'stop' THEN "Event_time" END )
      -min(CASE "Event_ID" WHEN 'start' THEN "Event_time" END )
      ) /60
      FROM
      some_table
      JOIN
      grouP BY "Correlation_ID"
      ;





      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        perhaps with a self join like this:



        SELECT
        start."Correlation_ID",
        extract(epoch from stop."Event_time" - start."Event_time")/60
        FROM
        some_table AS start
        JOIN
        some_table AS stop
        ON start."Correlation_ID"=finish."Correlation_ID"
        AND stop."Event_ID"='stop'
        WHERE
        start."Event_ID"='start'
        ;


        or with a group like this:



        SELECT
        "Correlation_ID",
        extract(epoch from
        max(CASE "Event_ID" WHEN 'stop' THEN "Event_time" END )
        -min(CASE "Event_ID" WHEN 'start' THEN "Event_time" END )
        ) /60
        FROM
        some_table
        JOIN
        grouP BY "Correlation_ID"
        ;





        share|improve this answer














        perhaps with a self join like this:



        SELECT
        start."Correlation_ID",
        extract(epoch from stop."Event_time" - start."Event_time")/60
        FROM
        some_table AS start
        JOIN
        some_table AS stop
        ON start."Correlation_ID"=finish."Correlation_ID"
        AND stop."Event_ID"='stop'
        WHERE
        start."Event_ID"='start'
        ;


        or with a group like this:



        SELECT
        "Correlation_ID",
        extract(epoch from
        max(CASE "Event_ID" WHEN 'stop' THEN "Event_time" END )
        -min(CASE "Event_ID" WHEN 'start' THEN "Event_time" END )
        ) /60
        FROM
        some_table
        JOIN
        grouP BY "Correlation_ID"
        ;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 4:38

























        answered Nov 12 at 4:31









        Jasen

        7,97611836




        7,97611836



























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53252483%2fpostgresql-time-difference-in-minutes-from-time-column%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







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands