sql - Get work time in minutes based on shift schedule -


in production have 3 shifts. each shift timing described in table tbl_shiftsched: enter image description here

wt - work time, pt - break time. shifttmid - schedule 2 , 3 shifts. looking easy way work time in minutes having start , end time. example, having input between #2015.05.29 06:10:00# , #2015.05.29 09:30:00# , tbl_workstations.wksid='grd' (workstation code relation on shifttmid ='3p') should give output 190 min.

i have function in ms access gives me needed output. when migrated t-sql becomes complicated because not find easy way how use alias in t-sql. here code:

    use [strdat]     go      declare     @strwks varchar(3),     @dtein datetime='2013.08.05 03:30',     @dteout datetime='2013.08.05 05:30',      @strshf varchar(12)=null,--'2013.08.04-3',      @strind varchar(2) = 'wt',     @dteftm datetime,     @dteshf date     --@prds datetime,     --@prde datetime       select top 1     @dteftm =      case         when @strshf not null          (select shiftstart tbl_shiftsched shifttmid=(select shifttiming tbl_workstations wksid=@strwks) , shift=right(@strshf,1) , sortind=1)         else @dtein-dateadd(day,datediff(day,0,@dtein),0) --cast(@dtein-cast(floor(@dtein) float) datetime)     end,     @dteshf=     case          when @strshf not null          left(@strshf,10)         else convert(varchar,dateadd(day,datediff(day,0,@dtein),0),102)      end      --select @dteftm,@dteshf      select tbl_shiftsched.shift,     tbl_shiftsched.sortind,      [shiftstart]+     case          when @dteftm>[shiftstart]         dateadd(day,1,@dteshf)         else @dteshf          end prds,     [shiftend]+     case         when @dteftm>[shiftend]         dateadd(day,1,@dteshf)         else @dteshf     end prde,     case         when @dtein>=[prds] , [prde]>=@dteout         datediff(minute,@dtein,@dteout)         else case             when @dtein<=[prds] , [prde]<=@dteout             datediff(minute,[prds],[prde])             else case                 when [prds]<=@dtein , @dtein<=[prde]                 datediff(minute,@dtein,[prde])                 else case                      when [prds]<=@dteout , @dteout<=[prde]                      datediff(minute,[prds],@dteout)                     else 0                     end                 end             end         end tm,     @dtein s,     @dteout e,     tbl_shiftsched.shifttype,tbl_shiftsched.shiftstart,tbl_shiftsched.shiftend      tbl_workstations      inner join tbl_shiftsched on tbl_workstations.shifttiming = tbl_shiftsched.shifttmid      (((tbl_workstations.wksid)=@strwks)) 

off course gives me error invalid column name 'prds' , 'prde' because use alias.

must more easy way achieve it. maybe on wrong direction?...

whenever have calculate field , use results in second field, use common table expression make first calculation. given query, this:

with cte_preprocess (     select tbl_shiftsched.shift,     tbl_shiftsched.sortind,      [shiftstart]+     case          when @dteftm>[shiftstart]         dateadd(day,1,@dteshf)         else @dteshf          end prds,     [shiftend]+     case         when @dteftm>[shiftend]         dateadd(day,1,@dteshf)         else @dteshf     end prde,     tbl_shiftsched.shifttype,tbl_shiftsched.shiftstart,tbl_shiftsched.shiftend      tbl_workstations      inner join tbl_shiftsched on tbl_workstations.shifttiming = tbl_shiftsched.shifttmid      (((tbl_workstations.wksid)=@strwks)) ) select [shift] , sortind , prds , prde , case         when @dtein>=[prds] , [prde]>=@dteout         datediff(minute,@dtein,@dteout)         else case             when @dtein<=[prds] , [prde]<=@dteout             datediff(minute,[prds],[prde])             else case                 when [prds]<=@dtein , @dtein<=[prde]                 datediff(minute,@dtein,[prde])                 else case                      when [prds]<=@dteout , @dteout<=[prde]                      datediff(minute,[prds],@dteout)                     else 0                     end                 end             end         end tm , @dtein , @dteout , shiftend cte_preprocess 

more on cte's here


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -