Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    712
    Thanks
    165
    Thanked 0 Times in 0 Posts

    Change syntax from Update Case Statement to Update Union Statement

    Good day!

    Theirs a programmer told me that could I used Union Statement to solve my issue in my query, but i can't imagine how can I do that, because my query I used case statement, I have no idea how can I used union instead of case statement.

    Here is my problem, I need to get the rendered hours and the rendered hours should be only 8 hours or less 8 hours if the employee was time in late.

    I have 3 shifts 21:35 - 05:35, 05:35- 13:35, 13:35-21:35

    using case statement it works only in one shift, I want to get the rendered with different scheduled.

    Like for example :
    Employee 1 time in = 21:00 time out = 05:40 the rendered = 8 hours only event he time in early and time out late it only compute the 21:35 - 05:35
    Employee 2 time in = 06:35 time out = 13:50 the rendered should be = 7 hours only because he was late 1 hour because his schedule is 05:35 but he time in 06:35 even he time out late its not considered because only time from 05:35-13:35 will get
    Employee 3 time in = 13:35 time out = 24:35 the rendered should be = 8 only even he timeout late.

    Rendered should be compute only the 8 hours of work, no matter he time out late only 8 hours would be get based on their schedule. and subtrace if he time in late.

    here is my syntax in case statement:

    PHP Code:
    UPDATE employee SET rendered sec_to_time(time_to_sec('08:00:00') + 
                case 
                  
    when time_to_sec(time(timein)) < time_to_sec('21:35:00'then 0
                  
    else time_to_sec('21:35:00') - time_to_sec(time(timein))
                
    end +
                case 
                  
    when time_to_sec(time(timeout)) > time_to_sec('05:35:00'then 0
                  
    else time_to_sec(time(timeout)) - time_to_sec('05:35:00')
                
    end
    it is only for one shift and it works, but when I tried this to get the rendered for 3 shifts:

    PHP Code:
    UPDATE employee SET rendered sec_to_time(time_to_sec('08:00:00') + 
                case 
                  
    when time_to_sec(time(timein)) < time_to_sec('21:35:00'then 0
                  
    else time_to_sec('21:35:00') - time_to_sec(time(timein))
                
    end +
                case 
                  
    when time_to_sec(time(timeout)) > time_to_sec('05:35:00'then 0
                  
    else time_to_sec(time(timeout)) - time_to_sec('05:35:00')
                
    end +
    case 
                  
    when time_to_sec(time(timein)) < time_to_sec('05:35:00'then 0
                  
    else time_to_sec('05:35:00') - time_to_sec(time(timein))
                
    end +
                case 
                  
    when time_to_sec(time(timeout)) > time_to_sec('13:35:00'then 0
                  
    else time_to_sec(time(timeout)) - time_to_sec('13:35:00')
                
    end +
        case 
                  
    when time_to_sec(time(timein)) < time_to_sec('13:35:00'then 0
                  
    else time_to_sec('13:35:00') - time_to_sec(time(timein))
                
    end +
                case 
                  
    when time_to_sec(time(timeout)) > time_to_sec('21:35:00'then 0
                  
    else time_to_sec(time(timeout)) - time_to_sec('21:35:00')
                
    end
    ); 
    wrong output was displayed.

    If UNION Statement is the solution? How? Thank you

    Is there any query can i used? I really need to solved this .

    Any help is highly appreciated.

    Thank you so much

  • #2
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    why do you bother with shifts at all. Calculate the in/out diff and that is it ?
    EDIT: aha, you ment 21:35 - 05:35, anything within that counts, outside that does not .....
    Last edited by BubikolRamios; 11-11-2011 at 05:40 PM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • Users who have thanked BubikolRamios for this post:

    newphpcoder (11-13-2011)

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    I suppose you have table with employee,in,out columns ?

    I don't know what all your functions are, what about using this (for start):

    TO_SECONDS() Return the date or datetime argument converted to seconds since Year 0
    that should help you to convert all to pure intiger operations.
    Last edited by BubikolRamios; 11-11-2011 at 05:51 PM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #4
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Ok looking a while at your code, and reading, are you sure you have good data,
    and did you consider the following logic (assuming all data are within expected range, which newer are in real life):

    Code:
    case when timein between 5:35 and 21:35 and timeout between 13:25 and  5:35
    case when timein between 13:25 and 5.35 ...
    case when timein between 21:32 and 13:25 ...

    when time_to_sec(time(timein)) < time_to_sec('21:35:00')
    timein 5.35 forinstance allso fits here (which you did notr think of I presume), hence probably bad result.

    If case works and does not give good result then probably case is good, logic is bad.
    Last edited by BubikolRamios; 11-11-2011 at 06:37 PM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • Users who have thanked BubikolRamios for this post:

    newphpcoder (11-14-2011)

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    712
    Thanks
    165
    Thanked 0 Times in 0 Posts
    I tried this code, can you check it if it is correct?



    PHP Code:
    UPDATE attendance SET Rendered sec_to_time(time_to_sec('08:00:00') + 
                case 
                  
    when time_to_sec(time(TimeIn)) < time_to_sec('05:35:00'then 0
                  
    else time_to_sec('05:35:00') - time_to_sec(time(TimeIn))
                
    end +
                case 
                  
    when time_to_sec(time(TimeOut)) > time_to_sec('13:35:00'then 0
                  
    else time_to_sec(time(TimeOut)) - time_to_sec('13:35:00')
                
    end
               
    WHERE shift 2;
    UPDATE attendance SET Rendered sec_to_time(time_to_sec('08:00:00') + 
                case 
                  
    when time_to_sec(time(TimeIn)) < time_to_sec('21:35:00'then 0
                  
    else time_to_sec('21:35:00') - time_to_sec(time(TimeIn))
                
    end +
                case 
                  
    when time_to_sec(time(TimeOut)) > time_to_sec('05:35:00'then 0
                  
    else time_to_sec(time(TimeOut)) - time_to_sec('05:35:00')
                
    end
               
    WHERE shift 1;
    UPDATE attendance SET Rendered sec_to_time(time_to_sec('08:00:00') + 
                case 
                  
    when time_to_sec(time(TimeIn)) < time_to_sec('13:35:00'then 0
                  
    else time_to_sec('13:35:00') - time_to_sec(time(TimeIn))
                
    end +
                case 
                  
    when time_to_sec(time(TimeOut)) > time_to_sec('21:35:00'then 0
                  
    else time_to_sec(time(TimeOut)) - time_to_sec('21:35:00')
                
    end
               
    WHERE shift 3;
    UPDATE attendance SET Rendered sec_to_time(time_to_sec('08:00:00') + 
                case 
                  
    when time_to_sec(time(TimeIn)) < time_to_sec('08:00:00'then 0
                  
    else time_to_sec('08:00:00') - time_to_sec(time(TimeIn))
                
    end +
                case 
                  
    when time_to_sec(time(TimeOut)) > time_to_sec('17:00:00'then 0
                  
    else time_to_sec(time(TimeOut)) - time_to_sec('17:00:00')
                
    end
               
    WHERE shift 4

    Thank you so much

  • #6
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    WHERE shift = 2;
    I see you invented shift, that should simplify things (if incoming data will be accurate.)

    Test with some sample data and you will see.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    712
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Good day!

    My boss told me that I no need to add field for shift, but I got a problem in my code for the shift 21:35-05:35 and also in 13:35-21:35 .
    here is the code:

    PHP Code:
      $result mysql_query("UPDATE attendance SET Rendered = sec_to_time(time_to_sec('08:00:00') + 
                case 
                  when time_to_sec(time(TimeIn)) < time_to_sec('21:35:00') then 0
                  else  time_to_sec('21:35:00') - time_to_sec(time(TimeIn))
                end +
                case 
                  when time_to_sec(time(TimeOut)) > time_to_sec('05:35:00') then 0
                  else time_to_sec(time(TimeOut)) - time_to_sec('05:35:00')
                end
               )"
    )
               or die(
    mysql_error());  

     
    $result mysql_query("UPDATE attendance SET Rendered = sec_to_time(time_to_sec('08:00:00') + 
                 case 
                  when time_to_sec(time(TimeIn)) < time_to_sec('13:35:00') then 0
                  else time_to_sec('13:35:00') - time_to_sec(time(TimeIn))
                end +
                case 
                  when time_to_sec(time(TimeOut)) > time_to_sec('21:35:00') then 0
                  else time_to_sec(time(TimeOut)) - time_to_sec('21:35:00')
                end 
               )"

     or die(
    mysql_error()); 
    i have time in : 2011-10-24 21:35:00 time out: 2011-10-25 08:35:00
    and the ouput in rendered using this code is: -14:00:00 it should be 08:00:00

    timein : 2011-10-24 13:35:00 timeout: 2011-10-24 21:35:00 rendered: 02:25:00 it should be 08:00:00

    Help me...Thank you so much


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •