I your first question, I gave you hints about what is wrong in your code.
How can I improve my SQL function that has 70+ conditional statements that computes shift schedules?[
^]
I thought your would have took advantage of them.
As long as you happily mix 2 different format for time, you can do any corrections or changes you want, nothing will work.
- First thing to do is to switch code to use a single time format for the whole query and probably all other code you use.
The SQL case gives the answer of first march, so you have to very carefully order all the conditions to get correct results.
So with @login ='3:00:00' and @logout ='13:30:00'n you will get 1 instead of 25 because of first match principle.
[Update]
Quote:
but i came up to another problem like what if this his/her shift but he will be on an OverTime,
You need to explain the whole problem you try to solve, context matters.
Quote:
can i use a looping structure to this case sir?
Probably, but I don't know what you want to do with the loop.
as you start to speak about OverTime, I have the feeling that the query is part of a bigger problem, and that the shift number is used for something else, I even suspect that you are on a bad solution.
Making the shifts non overlapping, is the good direction.
but this code:
set @shiftcode =(select case
when @login <='1:00:59' and @logout >='10:00:00' and @logout < '11:30:00' Then 22
when @login <='1:00:59' and @logout >='11:30:00' and @logout < '12:00:00' Then 29
when @login <='1:00:59' and @logout >='12:00:00'Then 51
when @login <='2:00:59' and @logout >='11:00:00' and @logout < '12:30:00' Then 77
when @login <='2:00:59' and @logout >='12:30:00' and @logout < '13:00:00' Then 30
when @login <='2:00:59' and @logout >='13:00:00'Then 52
can be simplified by a clever reordering:
set @shiftcode =(select case
when @login <='1:00:59' and @logout >='12:00:00' Then 51
when @login <='1:00:59' and @logout >='11:30:00' Then 29
when @login <='1:00:59' and @logout >='10:00:00' Then 22
when @login <='2:00:59' and @logout >='13:00:00' Then 52
when @login <='2:00:59' and @logout >='12:30:00' Then 30
when @login <='2:00:59' and @logout >='11:00:00' Then 77
Did you noticed that if someone leave early, you query fails?