Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.72 KB | None | 0 0
  1. declare @p_date date = getdate(),
  2.         @p_idRestaurant int = 44,
  3.         @firstDateOfWeek date = null,
  4.         @lastDateOfWeek date = null
  5.        
  6. set @firstDateOfWeek = dateadd(dd, (DATEPART(dw, @p_date) * -1) + 1, @p_date)
  7. set @lastDateOfWeek = dateadd(dd, 6, @firstDateOfWeek)
  8.  
  9. select  mec.id as idEmployeeContract,
  10.         me.id as idEmployee,
  11.         me.firstName,
  12.         me.lastName1,
  13.         me.lastName2,
  14.         r.id as idRestaurant,
  15.         r.bkCode,
  16.         r.name as restaurantName,
  17.         mecrw.dateStart,
  18.         mecrw.dateEnd,
  19.         (
  20.             select  isnull(sum(mecrwh2.totalHour), 0) as totalHour
  21.             from    mgtEmployeeContractRestaurantWorkingHour mecrwh2 with(nolock)
  22.             where   @p_date between @firstDateOfWeek and @lastDateOfWeek
  23.                     and mec.id = mecrwh2.idEmployeeContract
  24.         ) as totalAlreadyWorkingHourPerWeek,
  25.         mwtl.numberHour as numHourPerWeek,
  26.         mwtl.numberHour * 0.60 as numComplementaryHourPerWeek,
  27.         mecrwh.totalHour as numWorkingHourForDate,
  28.         mecrwh.idEmployeeStatus as idEmployeeStatus,
  29.         mes.*
  30. from    mgtEmployeeContract mec with(nolock)
  31. inner   join mgtEmployee me with(nolock)
  32.         on mec.idEmployee = me.id
  33. inner   join mgtWorkTimeList mwtl with(nolock)
  34.         on mec.idWorkTimeList = mwtl.id
  35. inner   join mgtEmployeeContractRestaurantWorking mecrw with(nolock)
  36.         on mec.id = mecrw.idEmployeeContract
  37. inner   join restaurant r with(nolock)
  38.         on mecrw.idRestaurantDestination = r.id
  39. left    join mgtEmployeeContractRestaurantWorkingHour mecrwh with(nolock)
  40.         on mec.id = mecrwh.idEmployeeContract
  41.         and mecrwh.date = @p_date
  42.         and mecrwh.idRestaurant = @p_idRestaurant
  43. left    join mgtEmployeeStatus mes with(nolock)
  44.         on mecrwh.idEmployeeStatus = mes.id
  45. where   @p_date between mecrw.dateStart and isnull(mec.dateEnd, '20991231')
  46.         and mec.indActive = 1
  47.         and mec.idRestaurant = @p_idRestaurant
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement