Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
midan1
Starting Member
39 Posts |
Posted - 2007-12-17 : 17:00:58
|
need help locate BUG in function !this my function and i have sum problem in itthis is the condition !case --Fridaywhen wday=6 and (select shiftType from rs d where d.rn=r.rn-1 and d.empid=r.empid)=2 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)<>2 then 2when wday=6 and (select shiftType from rs d where d.rn=r.rn-1 and d.empid=r.empid)=3 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)<>3 then 3--Saturdaywhen wday=7 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)=2 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)<>2 then 2when wday=7 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)=3 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)<>3 then 3else shiftType end as newShifTypefrom rs r) the problemit not symmetrical equal the shiftin this condition evry employee get (1,1,2,2,3,3,4,5)now if the "condition of Thursday" on value =2do this (1,1,2,2,2,3,4,5)give less "3"it must do this(1,1,2,2,2,3,3,4,5)and if the "condition of Thursday" on value =3do this (1,1,2,2,3,3,3,5) no 4 it subtract 4it must do this(1,1,2,2,3,3,3,4,5) for example if the employee don't get the value 44= rest at home - after 3 shift night he need to restthe all functionalter function shifts (@mth tinyint,@yr smallint)returns table asreturn(with ptrn as (select 1 as shiftType, 1 as prnunion allselect 1 ,2union allselect 2,3union allselect 2,4union allselect 3,5union allselect 3,6union allselect 4,7union allselect 5,8),emp as (select e.empid,f.date as basedate,datepart(dw,f.date ) as wday,row_number() over (partition by empid order by f.date ) as rn,unit = (DATEDIFF(MONTH, e.unit_date, f.[DATE])% 4) + 1from empbase e ,[dbo].[F_TABLE_DATE](dateadd(m,@mth-1,dateadd(yy,@yr-1900,0)),dateadd(m,@mth,dateadd(yy,@yr-1900,0))) fwhere f.DATE >= dateadd(m,@mth-1,dateadd(yy,@yr-1900,0))and f.DATE < dateadd(m,@mth,dateadd(yy,@yr-1900,0))),emp_r as (select empid,basedate,wday ,rn,unit,row_number() over (partition by empid,((rn-1)/8) order by basedate) as rndfrom emp),rs as (select * from emp_r einner join ptrn pon e.rnd=p.prn)select *,case --Fridaywhen wday=6 and (select shiftType from rs d where d.rn=r.rn-1 and d.empid=r.empid)=2 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)<>2 then 2when wday=6 and (select shiftType from rs d where d.rn=r.rn-1 and d.empid=r.empid)=3 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)<>3 then 3--Saturdaywhen wday=7 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)=2 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)<>2 then 2when wday=7 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)=3 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)<>3 then 3else shiftType end as newShifTypefrom rs r) TNX |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-17 : 17:04:13
|
And where is the sample data? And expected output?How could we else know if there is a bug or not? E 12°55'05.25"N 56°04'39.16" |
|
|
midan1
Starting Member
39 Posts |
Posted - 2007-12-17 : 17:26:38
|
seelook this is the right columnday in the week is after the the date left quote: 4807756 2007-01-01 00:00:00.000 1 1 1 1 1 1 14807756 2007-01-02 00:00:00.000 2 2 1 2 1 2 14807756 2007-01-03 00:00:00.000 3 3 1 3 2 3 24807756 2007-01-04 00:00:00.000 4 4 1 4 2 4 24807756 2007-01-05 00:00:00.000 5 5 1 5 3 5 34807756 2007-01-06 00:00:00.000 6 6 1 6 3 6 34807756 2007-01-07 00:00:00.000 7 7 1 7 4 7 34807756 2007-01-08 00:00:00.000 1 8 1 8 5 8 54807756 2007-01-09 00:00:00.000 2 9 1 1 1 1 14807756 2007-01-10 00:00:00.000 3 10 1 2 1 2 14807756 2007-01-11 00:00:00.000 4 11 1 3 2 3 24807756 2007-01-12 00:00:00.000 5 12 1 4 2 4 24807756 2007-01-13 00:00:00.000 6 13 1 5 3 5 34807756 2007-01-14 00:00:00.000 7 14 1 6 3 6 34807756 2007-01-15 00:00:00.000 1 15 1 7 4 7 44807756 2007-01-16 00:00:00.000 2 16 1 8 5 8 54807756 2007-01-17 00:00:00.000 3 17 1 1 1 1 14807756 2007-01-18 00:00:00.000 4 18 1 2 1 2 14807756 2007-01-19 00:00:00.000 5 19 1 3 2 3 24807756 2007-01-20 00:00:00.000 6 20 1 4 2 4 24807756 2007-01-21 00:00:00.000 7 21 1 5 3 5 24807756 2007-01-22 00:00:00.000 1 22 1 6 3 6 34807756 2007-01-23 00:00:00.000 2 23 1 7 4 7 44807756 2007-01-24 00:00:00.000 3 24 1 8 5 8 54807756 2007-01-25 00:00:00.000 4 25 1 1 1 1 14807756 2007-01-26 00:00:00.000 5 26 1 2 1 2 14807756 2007-01-27 00:00:00.000 6 27 1 3 2 3 24807756 2007-01-28 00:00:00.000 7 28 1 4 2 4 24807756 2007-01-29 00:00:00.000 1 29 1 5 3 5 34807756 2007-01-30 00:00:00.000 2 30 1 6 3 6 34807756 2007-01-31 00:00:00.000 3 31 1 7 4 7 49819590 2007-01-01 00:00:00.000 1 1 1 1 1 1 19819590 2007-01-02 00:00:00.000 2 2 1 2 1 2 19819590 2007-01-03 00:00:00.000 3 3 1 3 2 3 29819590 2007-01-04 00:00:00.000 4 4 1 4 2 4 29819590 2007-01-05 00:00:00.000 5 5 1 5 3 5 39819590 2007-01-06 00:00:00.000 6 6 1 6 3 6 39819590 2007-01-07 00:00:00.000 7 7 1 7 4 7 39819590 2007-01-08 00:00:00.000 1 8 1 8 5 8 59819590 2007-01-09 00:00:00.000 2 9 1 1 1 1 19819590 2007-01-10 00:00:00.000 3 10 1 2 1 2 19819590 2007-01-11 00:00:00.000 4 11 1 3 2 3 29819590 2007-01-12 00:00:00.000 5 12 1 4 2 4 29819590 2007-01-13 00:00:00.000 6 13 1 5 3 5 39819590 2007-01-14 00:00:00.000 7 14 1 6 3 6 39819590 2007-01-15 00:00:00.000 1 15 1 7 4 7 49819590 2007-01-16 00:00:00.000 2 16 1 8 5 8 59819590 2007-01-17 00:00:00.000 3 17 1 1 1 1 19819590 2007-01-18 00:00:00.000 4 18 1 2 1 2 19819590 2007-01-19 00:00:00.000 5 19 1 3 2 3 29819590 2007-01-20 00:00:00.000 6 20 1 4 2 4 29819590 2007-01-21 00:00:00.000 7 21 1 5 3 5 29819590 2007-01-22 00:00:00.000 1 22 1 6 3 6 39819590 2007-01-23 00:00:00.000 2 23 1 7 4 7 49819590 2007-01-24 00:00:00.000 3 24 1 8 5 8 59819590 2007-01-25 00:00:00.000 4 25 1 1 1 1 19819590 2007-01-26 00:00:00.000 5 26 1 2 1 2 19819590 2007-01-27 00:00:00.000 6 27 1 3 2 3 29819590 2007-01-28 00:00:00.000 7 28 1 4 2 4 29819590 2007-01-29 00:00:00.000 1 29 1 5 3 5 39819590 2007-01-30 00:00:00.000 2 30 1 6 3 6 39819590 2007-01-31 00:00:00.000 3 31 1 7 4 7 428895472 2007-01-01 00:00:00.000 1 1 1 1 1 1 128895472 2007-01-02 00:00:00.000 2 2 1 2 1 2 128895472 2007-01-03 00:00:00.000 3 3 1 3 2 3 228895472 2007-01-04 00:00:00.000 4 4 1 4 2 4 228895472 2007-01-05 00:00:00.000 5 5 1 5 3 5 328895472 2007-01-06 00:00:00.000 6 6 1 6 3 6 328895472 2007-01-07 00:00:00.000 7 7 1 7 4 7 328895472 2007-01-08 00:00:00.000 1 8 1 8 5 8 528895472 2007-01-09 00:00:00.000 2 9 1 1 1 1 128895472 2007-01-10 00:00:00.000 3 10 1 2 1 2 128895472 2007-01-11 00:00:00.000 4 11 1 3 2 3 228895472 2007-01-12 00:00:00.000 5 12 1 4 2 4 228895472 2007-01-13 00:00:00.000 6 13 1 5 3 5 328895472 2007-01-14 00:00:00.000 7 14 1 6 3 6 328895472 2007-01-15 00:00:00.000 1 15 1 7 4 7 428895472 2007-01-16 00:00:00.000 2 16 1 8 5 8 528895472 2007-01-17 00:00:00.000 3 17 1 1 1 1 128895472 2007-01-18 00:00:00.000 4 18 1 2 1 2 128895472 2007-01-19 00:00:00.000 5 19 1 3 2 3 228895472 2007-01-20 00:00:00.000 6 20 1 4 2 4 228895472 2007-01-21 00:00:00.000 7 21 1 5 3 5 228895472 2007-01-22 00:00:00.000 1 22 1 6 3 6 328895472 2007-01-23 00:00:00.000 2 23 1 7 4 7 428895472 2007-01-24 00:00:00.000 3 24 1 8 5 8 528895472 2007-01-25 00:00:00.000 4 25 1 1 1 1 128895472 2007-01-26 00:00:00.000 5 26 1 2 1 2 128895472 2007-01-27 00:00:00.000 6 27 1 3 2 3 228895472 2007-01-28 00:00:00.000 7 28 1 4 2 4 228895472 2007-01-29 00:00:00.000 1 29 1 5 3 5 328895472 2007-01-30 00:00:00.000 2 30 1 6 3 6 328895472 2007-01-31 00:00:00.000 3 31 1 7 4 7 451679900 2007-01-01 00:00:00.000 1 1 1 1 1 1 151679900 2007-01-02 00:00:00.000 2 2 1 2 1 2 151679900 2007-01-03 00:00:00.000 3 3 1 3 2 3 251679900 2007-01-04 00:00:00.000 4 4 1 4 2 4 251679900 2007-01-05 00:00:00.000 5 5 1 5 3 5 351679900 2007-01-06 00:00:00.000 6 6 1 6 3 6 351679900 2007-01-07 00:00:00.000 7 7 1 7 4 7 351679900 2007-01-08 00:00:00.000 1 8 1 8 5 8 551679900 2007-01-09 00:00:00.000 2 9 1 1 1 1 151679900 2007-01-10 00:00:00.000 3 10 1 2 1 2 151679900 2007-01-11 00:00:00.000 4 11 1 3 2 3 251679900 2007-01-12 00:00:00.000 5 12 1 4 2 4 251679900 2007-01-13 00:00:00.000 6 13 1 5 3 5 351679900 2007-01-14 00:00:00.000 7 14 1 6 3 6 351679900 2007-01-15 00:00:00.000 1 15 1 7 4 7 451679900 2007-01-16 00:00:00.000 2 16 1 8 5 8 551679900 2007-01-17 00:00:00.000 3 17 1 1 1 1 151679900 2007-01-18 00:00:00.000 4 18 1 2 1 2 151679900 2007-01-19 00:00:00.000 5 19 1 3 2 3 251679900 2007-01-20 00:00:00.000 6 20 1 4 2 4 251679900 2007-01-21 00:00:00.000 7 21 1 5 3 5 251679900 2007-01-22 00:00:00.000 1 22 1 6 3 6 351679900 2007-01-23 00:00:00.000 2 23 1 7 4 7 451679900 2007-01-24 00:00:00.000 3 24 1 8 5 8 551679900 2007-01-25 00:00:00.000 4 25 1 1 1 1 151679900 2007-01-26 00:00:00.000 5 26 1 2 1 2 151679900 2007-01-27 00:00:00.000 6 27 1 3 2 3 251679900 2007-01-28 00:00:00.000 7 28 1 4 2 4 251679900 2007-01-29 00:00:00.000 1 29 1 5 3 5 351679900 2007-01-30 00:00:00.000 2 30 1 6 3 6 351679900 2007-01-31 00:00:00.000 3 31 1 7 4 7 454693130 2007-01-01 00:00:00.000 1 1 1 1 1 1 154693130 2007-01-02 00:00:00.000 2 2 1 2 1 2 154693130 2007-01-03 00:00:00.000 3 3 1 3 2 3 254693130 2007-01-04 00:00:00.000 4 4 1 4 2 4 254693130 2007-01-05 00:00:00.000 5 5 1 5 3 5 354693130 2007-01-06 00:00:00.000 6 6 1 6 3 6 354693130 2007-01-07 00:00:00.000 7 7 1 7 4 7 354693130 2007-01-08 00:00:00.000 1 8 1 8 5 8 554693130 2007-01-09 00:00:00.000 2 9 1 1 1 1 154693130 2007-01-10 00:00:00.000 3 10 1 2 1 2 154693130 2007-01-11 00:00:00.000 4 11 1 3 2 3 254693130 2007-01-12 00:00:00.000 5 12 1 4 2 4 254693130 2007-01-13 00:00:00.000 6 13 1 5 3 5 354693130 2007-01-14 00:00:00.000 7 14 1 6 3 6 354693130 2007-01-15 00:00:00.000 1 15 1 7 4 7 454693130 2007-01-16 00:00:00.000 2 16 1 8 5 8 554693130 2007-01-17 00:00:00.000 3 17 1 1 1 1 154693130 2007-01-18 00:00:00.000 4 18 1 2 1 2 154693130 2007-01-19 00:00:00.000 5 19 1 3 2 3 254693130 2007-01-20 00:00:00.000 6 20 1 4 2 4 254693130 2007-01-21 00:00:00.000 7 21 1 5 3 5 254693130 2007-01-22 00:00:00.000 1 22 1 6 3 6 354693130 2007-01-23 00:00:00.000 2 23 1 7 4 7 454693130 2007-01-24 00:00:00.000 3 24 1 8 5 8 554693130 2007-01-25 00:00:00.000 4 25 1 1 1 1 154693130 2007-01-26 00:00:00.000 5 26 1 2 1 2 154693130 2007-01-27 00:00:00.000 6 27 1 3 2 3 254693130 2007-01-28 00:00:00.000 7 28 1 4 2 4 254693130 2007-01-29 00:00:00.000 1 29 1 5 3 5 354693130 2007-01-30 00:00:00.000 2 30 1 6 3 6 354693130 2007-01-31 00:00:00.000 3 31 1 7 4 7 455901961 2007-01-01 00:00:00.000 1 1 1 1 1 1 155901961 2007-01-02 00:00:00.000 2 2 1 2 1 2 155901961 2007-01-03 00:00:00.000 3 3 1 3 2 3 255901961 2007-01-04 00:00:00.000 4 4 1 4 2 4 255901961 2007-01-05 00:00:00.000 5 5 1 5 3 5 355901961 2007-01-06 00:00:00.000 6 6 1 6 3 6 355901961 2007-01-07 00:00:00.000 7 7 1 7 4 7 355901961 2007-01-08 00:00:00.000 1 8 1 8 5 8 555901961 2007-01-09 00:00:00.000 2 9 1 1 1 1 155901961 2007-01-10 00:00:00.000 3 10 1 2 1 2 155901961 2007-01-11 00:00:00.000 4 11 1 3 2 3 255901961 2007-01-12 00:00:00.000 5 12 1 4 2 4 255901961 2007-01-13 00:00:00.000 6 13 1 5 3 5 355901961 2007-01-14 00:00:00.000 7 14 1 6 3 6 355901961 2007-01-15 00:00:00.000 1 15 1 7 4 7 455901961 2007-01-16 00:00:00.000 2 16 1 8 5 8 555901961 2007-01-17 00:00:00.000 3 17 1 1 1 1 155901961 2007-01-18 00:00:00.000 4 18 1 2 1 2 155901961 2007-01-19 00:00:00.000 5 19 1 3 2 3 255901961 2007-01-20 00:00:00.000 6 20 1 4 2 4 255901961 2007-01-21 00:00:00.000 7 21 1 5 3 5 255901961 2007-01-22 00:00:00.000 1 22 1 6 3 6 355901961 2007-01-23 00:00:00.000 2 23 1 7 4 7 455901961 2007-01-24 00:00:00.000 3 24 1 8 5 8 555901961 2007-01-25 00:00:00.000 4 25 1 1 1 1 155901961 2007-01-26 00:00:00.000 5 26 1 2 1 2 155901961 2007-01-27 00:00:00.000 6 27 1 3 2 3 255901961 2007-01-28 00:00:00.000 7 28 1 4 2 4 255901961 2007-01-29 00:00:00.000 1 29 1 5 3 5 355901961 2007-01-30 00:00:00.000 2 30 1 6 3 6 355901961 2007-01-31 00:00:00.000 3 31 1 7 4 7 456157795 2007-01-01 00:00:00.000 1 1 1 1 1 1 156157795 2007-01-02 00:00:00.000 2 2 1 2 1 2 156157795 2007-01-03 00:00:00.000 3 3 1 3 2 3 256157795 2007-01-04 00:00:00.000 4 4 1 4 2 4 256157795 2007-01-05 00:00:00.000 5 5 1 5 3 5 356157795 2007-01-06 00:00:00.000 6 6 1 6 3 6 356157795 2007-01-07 00:00:00.000 7 7 1 7 4 7 356157795 2007-01-08 00:00:00.000 1 8 1 8 5 8 556157795 2007-01-09 00:00:00.000 2 9 1 1 1 1 156157795 2007-01-10 00:00:00.000 3 10 1 2 1 2 156157795 2007-01-11 00:00:00.000 4 11 1 3 2 3 256157795 2007-01-12 00:00:00.000 5 12 1 4 2 4 256157795 2007-01-13 00:00:00.000 6 13 1 5 3 5 356157795 2007-01-14 00:00:00.000 7 14 1 6 3 6 356157795 2007-01-15 00:00:00.000 1 15 1 7 4 7 456157795 2007-01-16 00:00:00.000 2 16 1 8 5 8 556157795 2007-01-17 00:00:00.000 3 17 1 1 1 1 156157795 2007-01-18 00:00:00.000 4 18 1 2 1 2 156157795 2007-01-19 00:00:00.000 5 19 1 3 2 3 256157795 2007-01-20 00:00:00.000 6 20 1 4 2 4 256157795 2007-01-21 00:00:00.000 7 21 1 5 3 5 256157795 2007-01-22 00:00:00.000 1 22 1 6 3 6 356157795 2007-01-23 00:00:00.000 2 23 1 7 4 7 456157795 2007-01-24 00:00:00.000 3 24 1 8 5 8 556157795 2007-01-25 00:00:00.000 4 25 1 1 1 1 156157795 2007-01-26 00:00:00.000 5 26 1 2 1 2 156157795 2007-01-27 00:00:00.000 6 27 1 3 2 3 256157795 2007-01-28 00:00:00.000 7 28 1 4 2 4 256157795 2007-01-29 00:00:00.000 1 29 1 5 3 5 356157795 2007-01-30 00:00:00.000 2 30 1 6 3 6 356157795 2007-01-31 00:00:00.000 3 31 1 7 4 7 456496581 2007-01-01 00:00:00.000 1 1 1 1 1 1 156496581 2007-01-02 00:00:00.000 2 2 1 2 1 2 156496581 2007-01-03 00:00:00.000 3 3 1 3 2 3 256496581 2007-01-04 00:00:00.000 4 4 1 4 2 4 256496581 2007-01-05 00:00:00.000 5 5 1 5 3 5 356496581 2007-01-06 00:00:00.000 6 6 1 6 3 6 356496581 2007-01-07 00:00:00.000 7 7 1 7 4 7 356496581 2007-01-08 00:00:00.000 1 8 1 8 5 8 556496581 2007-01-09 00:00:00.000 2 9 1 1 1 1 156496581 2007-01-10 00:00:00.000 3 10 1 2 1 2 156496581 2007-01-11 00:00:00.000 4 11 1 3 2 3 256496581 2007-01-12 00:00:00.000 5 12 1 4 2 4 256496581 2007-01-13 00:00:00.000 6 13 1 5 3 5 356496581 2007-01-14 00:00:00.000 7 14 1 6 3 6 356496581 2007-01-15 00:00:00.000 1 15 1 7 4 7 456496581 2007-01-16 00:00:00.000 2 16 1 8 5 8 556496581 2007-01-17 00:00:00.000 3 17 1 1 1 1 156496581 2007-01-18 00:00:00.000 4 18 1 2 1 2 156496581 2007-01-19 00:00:00.000 5 19 1 3 2 3 256496581 2007-01-20 00:00:00.000 6 20 1 4 2 4 256496581 2007-01-21 00:00:00.000 7 21 1 5 3 5 256496581 2007-01-22 00:00:00.000 1 22 1 6 3 6 356496581 2007-01-23 00:00:00.000 2 23 1 7 4 7 456496581 2007-01-24 00:00:00.000 3 24 1 8 5 8 556496581 2007-01-25 00:00:00.000 4 25 1 1 1 1 156496581 2007-01-26 00:00:00.000 5 26 1 2 1 2 156496581 2007-01-27 00:00:00.000 6 27 1 3 2 3 256496581 2007-01-28 00:00:00.000 7 28 1 4 2 4 256496581 2007-01-29 00:00:00.000 1 29 1 5 3 5 356496581 2007-01-30 00:00:00.000 2 30 1 6 3 6 356496581 2007-01-31 00:00:00.000 3 31 1 7 4 7 4
TNX |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-18 : 03:23:59
|
You're welcome. E 12°55'05.25"N 56°04'39.16" |
|
|
ilan
Starting Member
21 Posts |
Posted - 2007-12-18 : 04:49:56
|
Peso TNX |
|
|
ilan
Starting Member
21 Posts |
Posted - 2007-12-19 : 08:58:50
|
please help |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 09:03:04
|
We don't even know the business rules for calculating anything.1) Explain what you have2) Explain what you wantPost proper and accurate sample data in this formatDECLARE @Sample TABLE (Col1 INT, Col2 DATETIME, etc etc)INSERT @SampleSELECT 1, '20070801' UNION ALLSELECT 2, '20070901'Then post what the expected output is based on the above provided sample data.And last you explain the business rules how you come to that conclusion. E 12°55'05.25"N 56°04'39.16" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 09:12:57
|
I see other people as well has asked for the very same information I asked above... E 12°55'05.25"N 56°04'39.16" |
|
|
midan1
Starting Member
39 Posts |
Posted - 2007-12-19 : 19:56:59
|
i explain i have function that division for the employee shiftsgenerate daily shift pattern 1,1,2,2,3,3,4,51 =morning2=evening3=night4=rest5=homethis function go to table "empbase" check on filed "basedate" and last shift month that the employee finish !and from this point the function give to the employee shifts 1,1,2,2,3,3,4,5 for evry day one shift !if the employee on Thursday start shift 2 OR 3 the employee must finish the week-endfor exampleemployee ID=12345 get "work roster " for December from first of the month to the end of the monthfrom series of shifts 1,1,2,2,3,3,4,5 evry day oneand on Thursday if the employee start shift 2 or 3 the employee must finish the week-endThursday=2Friday=2Saturday=2ORThursday=3Friday=3Saturday=3and olso i have a rule so this functin do thisgenerate daily shift pattern 1,1,2,2,3,3,4,5,... my ruleif the employee get the shift 2 OR 3 on Thursday !!!but only if it Thursday !(the week-end start from Thursday until Sunday morning)the order for this employee id be 2,2,2 or 3,3,3i explain the employee must start the week-end and finish it with the same shiftbut only if it start a series 2 OR 3 (2=evening 3=night) ON Thursday .and after continue if the employee on Thursday start shift 2=evening than after 2,2,2 3,3,4,5,1,1,2,2,3,3,4,5,..if the employee on Thursday start shift 3=night than after 3,3,3 4,5,1,1,2,2,3,3,4,5,..so like this if the employee on Thursday start a series value 2 OR 3 the employee must to end it on the week-end from Thursday until Sunday morning-------------this is my table "empbase"the last date that the emplpyee work with last "shift" asn last UNIT date empid basedate unit_date shift ----------- ----------- ------- ----- 28895472 28/04/2007 00:00:00 01/01/2007 00:00:00 156496581 28/04/2007 00:00:00 01/01/2007 00:00:00 356157795 28/04/2007 00:00:00 01/01/2007 00:00:00 551679900 28/04/2007 00:00:00 01/01/2007 00:00:00 454693130 28/04/2007 00:00:00 01/01/2007 00:00:00 4 -----------------------------------so friends need saving-for this complex function can someone help ? from my boos to firing me !!and and olso get rid the function F_TABLE_DATE !TNX |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 21:25:11
|
The main problem here is that if you only go for the last date and that day's shift, you can't tell which shift to continue with.For example; the shift for last date is 2.How to you know if you should continue with the second 2-shift, or continue with first 3-shift? E 12°55'05.25"N 56°04'39.16" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 21:30:35
|
quote: Originally posted by midan1 so friends need saving-for this complex function can someone help ? from my boos to firing me !!
If you want to keep your job, then maybe you should listen to us and provide the information we request? E 12°55'05.25"N 56°04'39.16" |
|
|
ilan
Starting Member
21 Posts |
Posted - 2007-12-20 : 02:53:06
|
ok i think i fix this the code is below------------------------------------------------but i have main problem how to check the last shift + last date from my table "empbase"the problem in this line !!from empbase e ,[dbo].[F_TABLE_DATE](dateadd(m,@mth-1,dateadd(yy,@yr-1900,0)),dateadd(m,@mth,dateadd(yy,@yr-1900,0))) fwhere f.DATE >= dateadd(m,@mth-1,dateadd(yy,@yr-1900,0))and f.DATE < dateadd(m,@mth,dateadd(yy,@yr-1900,0)) my table ----------------------empid basedate unit_date shift ----------------------------------------------28895472 28/04/2007 28/01/2007 156496581 28/04/2007 30/01/2007 356157795 28/04/2007 11/01/2007 551679900 28/04/2007 12/01/2007 454693130 28/04/2007 10/01/2007 4 ----------------------------------------this is the last changes in my Function---------------------------------------USE [nili]GO/****** Object: UserDefinedFunction [dbo].[shif] Script Date: 12/19/2007 16:10:45 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER function [dbo].[shif] (@mth tinyint,@yr smallint)returns table asreturn(with ptrn as (select 1 as shiftType, 1 as prnunion allselect 1 ,2union allselect 2,3union allselect 2,4union allselect 3,5union allselect 3,6union allselect 4,7union allselect 5,8),emp as (select e.empid,f.date as basedate,datepart(dw,f.date ) as wday,row_number() over (partition by empid order by f.date ) as rn,unit = (DATEDIFF(MONTH, e.unit_date, f.[DATE])% 4) + 1from empbase e ,[dbo].[F_TABLE_DATE](dateadd(m,@mth-1,dateadd(yy,@yr-1900,0)),dateadd(m,@mth,dateadd(yy,@yr-1900,0))) fwhere f.DATE >= dateadd(m,@mth-1,dateadd(yy,@yr-1900,0))and f.DATE < dateadd(m,@mth,dateadd(yy,@yr-1900,0))),emp_r as (select empid,basedate,wday ,rn,unit,row_number() over (partition by empid,((rn-1)/8) order by basedate) as rndfrom emp),rs as (select * from emp_r einner join ptrn pon e.rnd=p.prn)select *,case --Fridaywhen wday=6 and (select shiftType from rs d where d.rn=r.rn-1 and d.empid=r.empid)=1 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)<>1 then 1when wday=6 and (select shiftType from rs d where d.rn=r.rn-1 and d.empid=r.empid)=2 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)<>2 then 2when wday=6 and (select shiftType from rs d where d.rn=r.rn-1 and d.empid=r.empid)=3 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)<>3 then 3--sunwhen wday=7 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)=1 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)<>1 then 1when wday=7 and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)=2 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)<>2 then 2when wday=7and (select shiftType from rs d where d.rn=r.rn-2 and d.empid=r.empid)=3 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)<>3 then 3--monwhen wday=1 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)=1 and (select shiftType from rs d where d.rn=r.rn-4 and d.empid=r.empid)<>1 then 2when wday=1 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)=2 and (select shiftType from rs d where d.rn=r.rn-4 and d.empid=r.empid)<>2 then 3when wday=1 and (select shiftType from rs d where d.rn=r.rn-3 and d.empid=r.empid)=3 and (select shiftType from rs d where d.rn=r.rn-4 and d.empid=r.empid)<>3 then 4when wday=2 and (select shiftType from rs d where d.rn=r.rn-4 and d.empid=r.empid)=1 and (select shiftType from rs d where d.rn=r.rn-5 and d.empid=r.empid)<>1 then 2else shiftType end as newShifTypefrom rs r) TNX |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-20 : 03:59:54
|
Exactly!How do you know that for this record28895472 28/04/2007 28/01/2007 1that the present "1" shift is the first or second "1" shift?If it is the first "1" shift, then there should be another "1" shift for the 29th of January.If it is the second "1" shift, then there should be a "2" shift for the 29th of January.Unless there are some business rules you haven't told us? E 12°55'05.25"N 56°04'39.16" |
|
|
ilan
Starting Member
21 Posts |
Posted - 2007-12-20 : 04:54:43
|
hi Pesothink i can do this no ??---------------------------------------------select id, max(Date), max(unit) ,max(ShiftID) from empbasegroup by id-------------------------------------------------like this i can know what is the last shift and start from this point no ?Peso can you help me to make peace this puzzle ?my problem that i have many prts of good codessee this code BUT it not suitable for all componentand it is not function !!-- need a list of employee ids with a basedate set to when they start with shift_code=1, unit=1-- this is a minimal tale to show the format-- extra columns could be added with other info (e.g. name) create table empbase (empid int,basedate datetime)-- fill with test datainsert empbase (empid,basedate) values (12345,'2007/1/1')insert empbase (empid,basedate) values (88877,'2007/1/5')insert empbase (empid,basedate) values (98765,'2007/1/20') insert empbase (empid,basedate) values (99994,'2007/6/5')go-------------------------------create function shifts (@mth tinyint,@yr smallint)returns@table_vartable (empid int, date datetime,shift_code int,unit int)as-- generate daily shift pattern 1,1,2,2,3,3,4,5,... changing units 1,2,3,4,... every 30 days.begindeclare @d1 datetimedeclare @d31 datetimeset @d1=convert(datetime,convert(char(8),@ yr*10000+@mth*100+1))set @d31=dateadd(dd,-1,dateadd(mm,1,@d1));with n01 (i) as (select 0 as 'i' union all select 1),seq (n) as (select d1.i+(2*d2.i)+(4*d3.i)+(8*d4.i)+(16*d5.i) as 'n'from n01 as d1cross joinn01 as d2cross join n01 as d3cross joinn01 as d4cross joinn01 as d5),dates (dt) as (selectdateadd(dd,n,@d1) as 'dt'fromseqwheredateadd(dd,n,@d1) <= @d31),modval (mod,val) as (select 0,1 union allselect 1,1 union allselect 2,2 union allselect 3,2 union allselect 4,3 union allselect 5,3 union allselect 6,4 union allselect 7,5)insert @table_varselectb.empid ,d.dt,(select val from modval where mod=(datediff(dd,b.basedate,d.dt) % 8)),((convert(int,(datediff(dd,b.basedate,d.dt) / 30)) % 4) + 1)fromempbase b, dates dwhereb.basedate <= d.dtreturn endgo -- test for various monthsselect * from shifts(1,2007) order by empid,dateselect * from shifts(2,2007) order by empid,dateselect * from shifts(3,2007) order by empid,dateselect * from shifts(4,2007) order by empid,date select * from shifts(5,2007) order by empid,dateselect * from shifts(12,2007) order by empid,date |
|
|
ilan
Starting Member
21 Posts |
Posted - 2007-12-20 : 06:02:42
|
yes i have onebusiness rulethis-------------------------------------------------------------unit = (DATEDIFF(MONTH, e.unit_date, f.[DATE])% 4) + 1--------------------------------------------------------the employee move location evry month from unit 1,2,3,4once a monththis line work ok 100% it change the location from the unit_dateTNx |
|
|
midan1
Starting Member
39 Posts |
Posted - 2007-12-21 : 07:52:47
|
maybe any genius can rescue me Friends! TNX for any help |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-21 : 08:06:25
|
No one can rescue you until you provide the information we need.We have asked for this informatin at least zillion of times. E 12°55'05.25"N 56°04'39.16" |
|
|
midan1
Starting Member
39 Posts |
Posted - 2007-12-21 : 08:53:28
|
Peso please try to understand me and help(my English is not perfect)i am desperate explanation i have only one table of employee with fields1 empid2 basedate3 shift 4 unit_date5 unit_valA)the field basedate + shiftis "reference" to give the employee to next month the shift order (1,1,2,2,3,3,4,5)so if the employee finish in shift=3then next month the employee start with shift=4B)the field unit_date + unit_valunit_date is "reference" to give the employee new location to next month only once a month 1 to 4 (1,2,3,4) evry month next numberC) i need to take care to fill the long weekend =Friday + Saturday + Sunday so if the employee start a long weekend with shift (but only if he start) shfit value 1 or 2 or 31=morning2=evening3=night ----------------------4=day rest5=homei perform to you the ideas and direction and olso examples of codes that don't working 100%if you think that is missing OKtry to help me to do itthis all the information Peso TNX |
|
|
midan1
Starting Member
39 Posts |
Posted - 2007-12-21 : 16:39:01
|
i think i have idea how to start the new shift month----------------------------------------this is the last day of the last monthselect id, max(Date), max(unit) ,max(ShiftID)from employeegroup by id---------------------------------------this is the day before last day in the last monthselect id, max(Date)-1, max(unit) ,max(ShiftID)from employeegroup by id so if i know the last day last month and day before last day in the monthi can do this---------------SELECT [new_shift_next_month] = CASE WHEN select id, max(Date)-1, max(unit)-1 ,max(ShiftID)-1from employeegroup by idShiftID=5andselect id, max(Date), max(unit) ,max(ShiftID)from employeegroup by idShiftID=1than [new_shift_next_month]=1 like thisif ShiftID (day before last day in the month)=5 ANDif ShiftID (day before last day in the month)=1 thennew_shift_next_month=1this is the orderif 51than 1------if11then 2if12then 2if22then 3if23then 3if33then 4if34then 5if45then 1---------------and after this i can go to the orginal order(1,1,2,2,3,3,4,5)TNXwait for any help |
|
|
Next Page
|
|
|
|
|