动态获取weekStart和weekEnd?

时间:2013-10-07 22:41:22

标签: sql stored-procedures

我花了很多时间试图弄清楚如何做到这一点却无法做到。所以请帮帮我。

我有这些数据:

ID  employee_id     worked_date     start_time      finish_time
 1     1          2013-09-25        09:00:00    17:30:00
 2     1          2013-09-26        07:00:00    17:00:00
 8     1          2013-10-01        09:00:00    17:00:00
 9     1          2013-10-04        09:00:00    17:00:00
 12    1          2013-10-07        09:00:00    17:00:00
 13    1          2013-10-10        09:00:00    17:00:00
 14    1          2013-10-11        09:00:00    17:00:00

我一周的第一天是星期三。基准日期是2013-09-25,即周三。我需要能够动态获取weekStart和weekEnd。例如,根据我上面的数据,2013-09-25至2013-10-11之间有三周时间。我先用最新的working_date命令我的数据。如果用户请求第3周,则weekStart应为2013-09-25,weekEnd应为2013-10-01。如果用户请求第2周,那么weekStart应为2013-10-02至2013-10-08,依此类推。

参数周将动态传递。谢谢你的帮助。

当请求的周为3时,我想要达到的结果是:

ID  employee_id     worked_date     start_time      finish_time  weekStart    weekEnd
 1     1          2013-09-25        09:00:00    17:30:00        2013-09-25    2013-10-01
 2     1          2013-09-26        07:00:00    17:00:00        2013-09-25    2013-10-01
 8     1          2013-10-01        09:00:00    17:00:00        2013-09-25    2013-10-01

使用此查询将为每条记录提供weekStart和weekEnd

SELECT *, dateadd(week, datediff(day,'20000105',worked_date) / 7, '20000105') AS WeekStart , 
          dateadd(week, datediff(day,'20000105',worked_date) / 7, '20000105') + 6 AS WeekEnd 
FROM Timesheet

但我不想要这个。我只想要用户要求的特定周。

2 个答案:

答案 0 :(得分:2)

听起来你可能需要一张日历表。

create table calendar (
calendarId int identity(1,1) primary key,
year int,
month int,
week int,
startDate date,
endDate date)
然后你可以说'给我第2周的所有记录'

declare @week int = 2
declare @year int = 2013

;with employeeCalendar as (
  select
    employee.employeeid
   ,startDate
   ,endDate 
  from 
    employee 
  cross apply 
    calendar
  where 
    calendar.week = @week ) 
select
  employeeCalendar.EmployeeId
 ,employeeShift.[date]
 ,start_time
 ,finish_time
 ,startDate 
 ,endDate 
from
  employeeCalendar
left join
  employeeShift 
on employeeShift.employeeid = employeeCalendar.employeeid 
and employeeShift.worked_date 
between employeeCalendar.startDate and employeeCalendar.endDate

您可以使用如下查询填充该表:

;with calendarCte as (
select
  1 as week
 ,convert(date,'2013-09-25') as startDate
 ,convert(date,'2013-10-01') as endDate
 union all
 select
  week + 1
 ,dateadd(week,1,startDate)
 ,dateadd(week,1,endDate)
 from
  calendarCte 
 where
  calendarCte.startDate < convert(date,'2043-09-25') ) 
insert into calendar( [year],[month],[week],startdate,stopdate)
 select 
  datepart(year,startDate) as [year] 
 ,datepart(month,startDate) as [month]
 ,week as [week]
 ,startDate
 ,endDate
 from calendarCte option (maxrecursion 0)

答案 1 :(得分:1)

您说您正在寻找存储过程。你在寻找这样的东西吗?

CREATE PROCEDURE ProcedureName @week int AS

SELECT * FROM Timesheet
WHERE worked_date >= dateadd(week, @week, '2013-09-25')
AND worked_date < dateadd(day,7,dateadd(week, @week, '2013-09-25'))

如果你想要你的weekStart和weekEnd,你可以像以前一样将它们添加到SELECT中。

如果您希望从当前周开始按降序排列,则可以按如下方式编写。

CREATE PROCEDURE ProcedureName @week int AS

SELECT * FROM Timesheet
WHERE worked_date >= dateadd(week, -@week, '2013-10-16')
AND worked_date < dateadd(day,7,dateadd(week, -@week, '2013-10-16'))

您还可以参数化传入存储过程的日期,因为当前周将始终在变化。