-2

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

我有这个数据:

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 年 9 月 25 日,即星期三。我需要能够动态获取 weekStart 和 weekEnd 。例如,根据我上面的数据,2013-09-25 到 2013-10-11 之间有三周。我首先使用最新的工作日期订购我的数据。如果用户请求第 3 周,则 weekStart 应为 2013-09-25,weekEnd 应为 2013-10-01。如果用户请求 week2,则 weekStart 应该是 2013-10-02 到 2013-10-08 等等。

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

这是我在请求的第 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

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

4

2 回答 2

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)
于 2013-10-07T23:24:03.803 回答
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'))

您还可以参数化您传递给存储过程的日期,因为当前周总是在变化。

于 2013-10-08T01:54:55.823 回答