Как я могу улучшить свою функцию SQL? С 70 условных операторов
This function is for the system I debug which is need to be updated.The system before will manually input shift scheduling for the employees, but the problem is, it is time consuming because of the volume of employees, and these employee's will change their shift almost everyday/week, so another change of their schedule will be done manually, this function or update that i will be making is intended to automatically assign the shifts from the comparison of their 'time in' or 'time out', actually the function works properly because it will literally computes the timein/out and were assigned properly but that if the employee will time in an hour before his shift and also an hour after his shift the function will be fine , but the problems is what if an Employee will time in late for his shift so automatically another shift will be assign to him base from the function and also for the timeout if ever he/she will work OverTime,
- это предел моей функции, надеюсь, у вас, ребята, есть идея получше, так как я тоже работаю над своей идеей и прихожу к решению, которое решит эту проблему,
Вот моя забава:
CREATE function [dbo].[fn_ComputeShiftCode] ( @login time, @logout time ) returns int as begin declare @shiftcode int <pre> set @shiftcode =(select case --1 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 2 --2 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 --3 when @login <='3:00:59' and @logout >='14:00:00' Then 53 when @login <='3:00:59' and @logout >='13:30:00' Then 25 when @login <='3:00:59' and @logout >='12:00:00' Then 20 --4 when @login <='4:00:59' and @logout >='15:00:00'Then 54 when @login <='4:00:59' and @logout >='14:30:00'Then 27 when @login <= '4:00:59' and @logout >= '13:00:00'Then 1 --5 when @login <='5:00:59' and @logout >='16:00:00'Then 55 when @login <='5:00:59' and @logout >='15:30:00' Then 26 when @login <='5:00:59' and @logout >='15:30:00' Then 31 when @login <='5:00:59' and @logout >= '14:00:00' Then 2 --6 when @login <='6:00:59' and @logout >= '17:00:00' then 4 when @login <='6:00:59' and @logout >='17:00:00'Then 56 when @login <='6:00:59' and @logout >='16:30:00' Then 32 when @login <='6:00:59' and @logout >= '15:00:00' then 3 --7 when @login <='7:00:59' and @logout >='18:00:00'Then 57 when @login <='7:00:59' and @logout >='17:30:00'Then 33 when @login <='7:00:59' and @logout >= '17:00:00' then 7 when @login <='7:00:59' and @logout >= '16:00:00' then 6 when @login <='7:00:59' and @logout >='14:00:00' Then 80 when @login <='7:00:59' and @logout >= '14:00:00' then 5 --8 when @login <='8:00:59' and @logout >='19:00:00' Then 9 when @login <='8:00:59' and @logout >='19:00:00'Then 58 when @login <='8:00:59' and @logout >='18:30:00' Then 34 when @login <='8:00:59' and @logout >='18:30:00' Then 85 when @login <='8:00:59' and @logout >='18:00:00'Then 86 when @login <='8:00:59' and @logout >='17:00:00' Then 8 --9 when @login <='9:00:59' and @logout >='20:00:00'Then 59 when @login <='9:00:59' and @logout >='19:30:00' Then 35 when @login <='9:00:59' and @logout >='18:00:00' Then 10 --10 when @login <='10:00:59' and @logout >='21:00:00'Then 60 when @login <='10:00:59' and @logout >='20:30:00'Then 36 when @login <='10:00:59' and @logout >='19:00:00' Then 23 --11 when @login <='11:00:59' and @logout >='22:00:00'Then 61 when @login <='11:00:59' and @logout >='21:30:00'Then 37 when @login <='11:00:59' and @logout >='20:00:00'Then 76 --12 when @login <='12:00:59' and @logout >='23:00:00'Then 74 when @login <='12:00:59' and @logout >='22:30:00' Then 38 when @login <='12:00:59' and @logout >='21:00:00' Then 24 when @login <='12:00:59' and @logout >='10:30:00' Then 50 --13 when @login <='13:00:59' and @logout >='23:30:00' Then 39 when @login <='13:00:59' and @logout >='22:00:00' Then 21 when @login <='13:00:59' and @logout >='0:00:00' Then 63 --14 when @login <='14:00:59' and @logout >='23:00:00'Then 12 when @login <='14:00:59' and @logout >='21:00:00' Then 11 when @login <='14:00:59' and @logout >='21:00:00' Then 81 when @login <='14:00:59' and @logout >='1:00:00' Then 64 when @login <='14:00:59' and @logout >='0:30:00' Then 40 --15 when @login <='15:00:59' and @logout >='2:00:00' Then 65 when @login <='15:00:59' and @logout >='1:30:00' Then 41 when @login <='15:00:59' and @logout >='0:00:00' Then 18 --16 when @login <='16:00:59' and @logout >='23:00:00'Then 79 when @login <='16:00:59' and @login >='15:00:00'Then 13 when @login <='16:00:59' and @logout >='3:00:00' Then 66 when @login <='16:00:59' and @logout >='2:30:00' Then 42 --17 when @login <='17:00:59' and @logout >='4:00:00' Then 67 when @login <='17:00:59' and @logout >='3:30:00' Then 43 when @login <='17:00:59' and @logout >='2:00:00' Then 14 when @login <='17:00:59' and @logout >='0:00:00' Then 84 --18 when @login <='18:00:59' and @logout >='5:00:00' Then 68 when @login <='18:00:59' and @logout >='4:30:00' Then 44 when @login <='18:00:59' and @logout >='3:00:00' Then 15 --19 when @login <='19:00:59' and @logout >='6:00:00' Then 69 when @login <='19:00:59' and @logout >='5:30:00' Then 45 when @login <='19:00:59' and @logout >='4:00:00' Then 19 --20 when @login <='20:00:59' and @logout >='7:00:00' Then 70 when @login <='20:00:59' and @logout >='6:30:00' Then 46 when @login <='20:00:59' and @logout >='5:00:00' Then 17 --21 when @login <='21:00:59' and @logout >='8:00:00' Then 71 when @login <='21:00:59' and @logout >='7:30:00' Then 47 when @login <='21:00:59' and @logout >='6:00:00' Then 16 when @login <='21:00:59' and @logout >='4:00:00' Then 82 --22 when @login <='22:00:59' and @logout >='9:00:00' Then 72 when @login <='22:00:59' and @logout >='8:30:00'Then 48 when @login <='22:00:59' and @logout >='7:00:00' Then 75 when @login <='22:00:59' and @logout >='5:00:00' Then 83 --23 when @login <='23:00:59' and @logout >='10:00:00'Then 73 when @login <='23:00:59' and @logout >='9:30:00' Then 49 --0000 when @login >='23:00:59' and @login <='0:00:59' and @logout >='23:00:00'Then 28 when @login >='23:00:59' and @login <='0:00:59' and @logout >='11:00:00' Then 62 when @login >='23:00:59' and @login <='0:00:59' and @logout >='9:00:00' Then 78 else 0 end ) return @shiftcode end
Вот мои shif_codes и соответствующие им shift_names/Shift_schedule
Цитата:Идентификатор сменности - график сменности
С 1 4 утра до 1 часа дня
С 2 5 утра до 2 часов дня
С 3 6 утра до 3 вечера
С 4 6 утра до 5 вечера
С 5 7 утра до 2 часов дня
С 6 7 утра до 4 вечера
С 7 утра до 5 вечера
С 8 утра до 5 вечера
С 9 8 утра до 7 вечера
С 10 9 утра до 6 вечера
11 С 2 вечера до 9 вечера
12 С 2 вечера до 11 вечера
13 С 4 вечера до 1 часа ночи
14 С 5 вечера до 2 часов ночи
15 с 6 вечера до 3 утра
16 9 вечера до 6 утра
17 С 8 вечера до 5 утра
18 С 3 вечера до 12 утра
19 С 7 вечера до 4 утра
20 с 3 утра до 12 вечера
23 С 10 утра до 7 вечера
24 с 12 вечера до 9 вечера
25 3 утра до 1:30pm_9.5hrs
26 5 утра до 3:30pm9.5hrs
28 С 12 утра до 11 вечера _10 часов.
32 С 6 утра до 4:30pm_9.5hrs
33 с 7 утра до 5:30pm_9.5hrs
34 с 8 утра до 6:30pm_9.5hrs
35 с 9 утра до 7:30pm_9.5hrs
36 10 утра до 8:30pm_9.5hrs
37 11amto9:30pm-9.5 hrs
38 С 12 утра до 10:30_9.5
39 1pmto11:30pm_9.5hrs
40 С 2 вечера до 12:30 утра-9,5 часа
41 с 3 вечера до 1:30 утра
42 с 4 вечера до 2:30 утра_9.5hrs
43 с 5 вечера до 3:30 утра_9.5hrs
44 с 6 вечера до 4:30 утра_9.5hrs
45 7 вечера до 5:30 утра_9.5hrs
46 с 8 вечера до 6:30 утра_9.5hrs
47 с 9 вечера до 7:30 утра_9.5hrs
48 с 10 вечера до 8:30 утра_9.5hrs
49 11 вечера до 9:30 утра_9.5hrs
50 с 12 вечера до 10:30 утра_9.5hrs
51 с 1 утра до 12:00pm_10hrs
52 с 2 утра до 1:00pm_10hrs
53 3 часа ночи, чтобы 2pm_10hrs
54 4 утра to3pm_10hrs
55 с 5 утра до 4pm_10hrs
56 с 6 утра до 5pm_10hrs
57 с 7 утра до 6pm_10hrs
58 с 8 утра до 7pm_10hrs
59 с 9 утра до 8pm_10hrs
60 10 утра до 9pm_10hrs
61 с 11 утра до 10 вечера_10 часов
62 12 утра до 11am_10hrs
63 1 вечера до 12am_10hrs
64 с 2 до 1am_10hrs
65 3 вечера до 2am_10hrs
66 4 вечера до 3am_10hrs
67 5 вечера до 4am_10hrs
68 6 вечера до 5am_10hrs
69 с 7 вечера до 6 утра_10 часов
70 с 8 вечера до 7 утра_10 часов
71 9 вечера до 8am_10hrs
72 10 до 9am_10hrs
73 11 вечера до 10am_10hrs
21 С 1 вечера до 10 вечера
22 1 ночи до 10 утра
75 с 10 вечера до 7 утра
76 с 11 утра до 8 вечера
77 2 часов ночи до 11 утра
78 с 12 утра до 9 утра
79 с 4 вечера до 11 вечера(6 часов)
80 с 7 утра до 2 часов дня(6 часов)
81 с 2 вечера до 9 вечера(6 часов)
82 с 9 вечера до 4 утра(6 часов)
83 с 10 вечера до 5 утра(6 часов)
84 с 5 вечера до 12 утра(6 часов)
85 с 8 утра до 6:30 вечера комп
86 8amto6:00pmcomp(9.5)
27 4 утра до 2:30pm_9.5hrs
29 С 1 утра до 11:30 утра 9.5 часов
30 2amto12:30pm_9.5hrs
31 5 утра до 3:30pm_9.5hrs
74 12 до 11pm_10hrs
Что я уже пробовал:
Я попытаюсь изменить свою функцию, чтобы вместо сравнения времени входа и выхода я просто сравнивал ее с временем входа или временем начала,
Richard Deeming
Репост
Вы уже опубликовали это сообщение:
https://www.codeproject.com/Questions/1216434/How-can-I-improve-my-SQL-function-that-has-plus-co[^]
Patrice T
Почти репост, ошибка первого вопроса была решена.