How to calculate short hour in and short hour out in postgresql?











up vote
0
down vote

favorite












id    checkin   checkout
------------------------
1 12:27 18:10
2 11:00 19:59


I want this result and out checkin time fix 11:00:00
and checkout time fix 20:00:00.



i am using this query



 select min(checktime)::time as checkin, 
max(checktime)::time as checkout,
(case
when cast(min (a.checktime) as time) > '11:00:00' then
(extract (epoch from min (a.checktime)::time - '11:00:00')/3600 )
else 0
end) as shorthour_in ,
(case
when cast(max (a.checktime) as time) <= '20:00:00'
then (extract (epoch from '20:00:00' - max (a.checktime)::time )/3600 )
else 0
end) as shorthour_out
from attendance_FHLHR


id checkin checkout shorthour_in shorthour_out
---------------------------------------------------------
1 12:27 18:10 1.45 1.83
2 11:00 19:59 0 0.016


can any one guide me is it correct query to get / calculate hours?










share|improve this question
























  • Properly formatted SQL is so much easier to read and understand.
    – jarlh
    Nov 15 at 7:04










  • Please share a sample input and expected output.
    – Mayank Porwal
    Nov 15 at 7:07










  • @MayankPorwal i write expected output. see question completely
    – Amna Kalsoom
    Nov 15 at 7:07






  • 1




    What data type are the columns checkin and checkout? The cast() seems to implicate you are storing a time value in a varchar column.
    – a_horse_with_no_name
    Nov 15 at 7:08










  • And what is the input for this output?
    – Mayank Porwal
    Nov 15 at 7:09















up vote
0
down vote

favorite












id    checkin   checkout
------------------------
1 12:27 18:10
2 11:00 19:59


I want this result and out checkin time fix 11:00:00
and checkout time fix 20:00:00.



i am using this query



 select min(checktime)::time as checkin, 
max(checktime)::time as checkout,
(case
when cast(min (a.checktime) as time) > '11:00:00' then
(extract (epoch from min (a.checktime)::time - '11:00:00')/3600 )
else 0
end) as shorthour_in ,
(case
when cast(max (a.checktime) as time) <= '20:00:00'
then (extract (epoch from '20:00:00' - max (a.checktime)::time )/3600 )
else 0
end) as shorthour_out
from attendance_FHLHR


id checkin checkout shorthour_in shorthour_out
---------------------------------------------------------
1 12:27 18:10 1.45 1.83
2 11:00 19:59 0 0.016


can any one guide me is it correct query to get / calculate hours?










share|improve this question
























  • Properly formatted SQL is so much easier to read and understand.
    – jarlh
    Nov 15 at 7:04










  • Please share a sample input and expected output.
    – Mayank Porwal
    Nov 15 at 7:07










  • @MayankPorwal i write expected output. see question completely
    – Amna Kalsoom
    Nov 15 at 7:07






  • 1




    What data type are the columns checkin and checkout? The cast() seems to implicate you are storing a time value in a varchar column.
    – a_horse_with_no_name
    Nov 15 at 7:08










  • And what is the input for this output?
    – Mayank Porwal
    Nov 15 at 7:09













up vote
0
down vote

favorite









up vote
0
down vote

favorite











id    checkin   checkout
------------------------
1 12:27 18:10
2 11:00 19:59


I want this result and out checkin time fix 11:00:00
and checkout time fix 20:00:00.



i am using this query



 select min(checktime)::time as checkin, 
max(checktime)::time as checkout,
(case
when cast(min (a.checktime) as time) > '11:00:00' then
(extract (epoch from min (a.checktime)::time - '11:00:00')/3600 )
else 0
end) as shorthour_in ,
(case
when cast(max (a.checktime) as time) <= '20:00:00'
then (extract (epoch from '20:00:00' - max (a.checktime)::time )/3600 )
else 0
end) as shorthour_out
from attendance_FHLHR


id checkin checkout shorthour_in shorthour_out
---------------------------------------------------------
1 12:27 18:10 1.45 1.83
2 11:00 19:59 0 0.016


can any one guide me is it correct query to get / calculate hours?










share|improve this question















id    checkin   checkout
------------------------
1 12:27 18:10
2 11:00 19:59


I want this result and out checkin time fix 11:00:00
and checkout time fix 20:00:00.



i am using this query



 select min(checktime)::time as checkin, 
max(checktime)::time as checkout,
(case
when cast(min (a.checktime) as time) > '11:00:00' then
(extract (epoch from min (a.checktime)::time - '11:00:00')/3600 )
else 0
end) as shorthour_in ,
(case
when cast(max (a.checktime) as time) <= '20:00:00'
then (extract (epoch from '20:00:00' - max (a.checktime)::time )/3600 )
else 0
end) as shorthour_out
from attendance_FHLHR


id checkin checkout shorthour_in shorthour_out
---------------------------------------------------------
1 12:27 18:10 1.45 1.83
2 11:00 19:59 0 0.016


can any one guide me is it correct query to get / calculate hours?







sql postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 7:08









a_horse_with_no_name

289k46438533




289k46438533










asked Nov 15 at 7:03









Amna Kalsoom

276




276












  • Properly formatted SQL is so much easier to read and understand.
    – jarlh
    Nov 15 at 7:04










  • Please share a sample input and expected output.
    – Mayank Porwal
    Nov 15 at 7:07










  • @MayankPorwal i write expected output. see question completely
    – Amna Kalsoom
    Nov 15 at 7:07






  • 1




    What data type are the columns checkin and checkout? The cast() seems to implicate you are storing a time value in a varchar column.
    – a_horse_with_no_name
    Nov 15 at 7:08










  • And what is the input for this output?
    – Mayank Porwal
    Nov 15 at 7:09


















  • Properly formatted SQL is so much easier to read and understand.
    – jarlh
    Nov 15 at 7:04










  • Please share a sample input and expected output.
    – Mayank Porwal
    Nov 15 at 7:07










  • @MayankPorwal i write expected output. see question completely
    – Amna Kalsoom
    Nov 15 at 7:07






  • 1




    What data type are the columns checkin and checkout? The cast() seems to implicate you are storing a time value in a varchar column.
    – a_horse_with_no_name
    Nov 15 at 7:08










  • And what is the input for this output?
    – Mayank Porwal
    Nov 15 at 7:09
















Properly formatted SQL is so much easier to read and understand.
– jarlh
Nov 15 at 7:04




Properly formatted SQL is so much easier to read and understand.
– jarlh
Nov 15 at 7:04












Please share a sample input and expected output.
– Mayank Porwal
Nov 15 at 7:07




Please share a sample input and expected output.
– Mayank Porwal
Nov 15 at 7:07












@MayankPorwal i write expected output. see question completely
– Amna Kalsoom
Nov 15 at 7:07




@MayankPorwal i write expected output. see question completely
– Amna Kalsoom
Nov 15 at 7:07




1




1




What data type are the columns checkin and checkout? The cast() seems to implicate you are storing a time value in a varchar column.
– a_horse_with_no_name
Nov 15 at 7:08




What data type are the columns checkin and checkout? The cast() seems to implicate you are storing a time value in a varchar column.
– a_horse_with_no_name
Nov 15 at 7:08












And what is the input for this output?
– Mayank Porwal
Nov 15 at 7:09




And what is the input for this output?
– Mayank Porwal
Nov 15 at 7:09












1 Answer
1






active

oldest

votes

















up vote
1
down vote













select min (  checktime )::time as checkin, 
max (checktime)::time as checkout,
(case when cast(min (checktime) as time) >= '11:00:00' then
(extract (epoch from min (checktime)::time - '11:00:00')/3600 ) else 0 end) as shorthour_in ,
(case when cast(max ( checktime) as time) <= '20:00:00' then
(extract (epoch from '20:00:00' - max ( checktime)::time )/3600 ) else 0 end) as shorthour_out
from attendance_FHLHR


check this answer.






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%2f53314057%2fhow-to-calculate-short-hour-in-and-short-hour-out-in-postgresql%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













    select min (  checktime )::time as checkin, 
    max (checktime)::time as checkout,
    (case when cast(min (checktime) as time) >= '11:00:00' then
    (extract (epoch from min (checktime)::time - '11:00:00')/3600 ) else 0 end) as shorthour_in ,
    (case when cast(max ( checktime) as time) <= '20:00:00' then
    (extract (epoch from '20:00:00' - max ( checktime)::time )/3600 ) else 0 end) as shorthour_out
    from attendance_FHLHR


    check this answer.






    share|improve this answer

























      up vote
      1
      down vote













      select min (  checktime )::time as checkin, 
      max (checktime)::time as checkout,
      (case when cast(min (checktime) as time) >= '11:00:00' then
      (extract (epoch from min (checktime)::time - '11:00:00')/3600 ) else 0 end) as shorthour_in ,
      (case when cast(max ( checktime) as time) <= '20:00:00' then
      (extract (epoch from '20:00:00' - max ( checktime)::time )/3600 ) else 0 end) as shorthour_out
      from attendance_FHLHR


      check this answer.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        select min (  checktime )::time as checkin, 
        max (checktime)::time as checkout,
        (case when cast(min (checktime) as time) >= '11:00:00' then
        (extract (epoch from min (checktime)::time - '11:00:00')/3600 ) else 0 end) as shorthour_in ,
        (case when cast(max ( checktime) as time) <= '20:00:00' then
        (extract (epoch from '20:00:00' - max ( checktime)::time )/3600 ) else 0 end) as shorthour_out
        from attendance_FHLHR


        check this answer.






        share|improve this answer












        select min (  checktime )::time as checkin, 
        max (checktime)::time as checkout,
        (case when cast(min (checktime) as time) >= '11:00:00' then
        (extract (epoch from min (checktime)::time - '11:00:00')/3600 ) else 0 end) as shorthour_in ,
        (case when cast(max ( checktime) as time) <= '20:00:00' then
        (extract (epoch from '20:00:00' - max ( checktime)::time )/3600 ) else 0 end) as shorthour_out
        from attendance_FHLHR


        check this answer.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 at 7:09









        Zeshan

        381110




        381110






























            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%2f53314057%2fhow-to-calculate-short-hour-in-and-short-hour-out-in-postgresql%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







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents