...

View Full Version : Change syntax from Update Case Statement to Update Union Statement



newphpcoder
11-11-2011, 02:26 AM
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:



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:



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

BubikolRamios
11-11-2011, 06:37 PM
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 .....

BubikolRamios
11-11-2011, 06:48 PM
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.

BubikolRamios
11-11-2011, 07:26 PM
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):



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.

newphpcoder
11-14-2011, 12:46 AM
I tried this code, can you check it if it is correct?





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

BubikolRamios
11-14-2011, 03:42 AM
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.

newphpcoder
11-14-2011, 10:19 AM
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:



$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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum