每周更新同一表中的列,以基于前一周的计算和

时间:2015-07-16 17:27:10

标签: sql-server sql-update

我正在尝试编写一个商店程序,每周更新上周同一个表中的WeeklytotalHours列。 我有时钟表,有暂停项目

TcEmployeeID Int
TcDate Date
ClockIn Datetime
ClockOut Datetime
TcTotalHours (Computed column calculated daily hours)
WeeklytotalHours

我尝试了这个问题:

SELECT 
TcEmployeeID, MAX(TcDate) As LastClockInDate, 
(sum(TcTotalHours) / 60   + (sum(TcTotalHours) % 60) / 100.0) as WeeklytotalHours 
FROM tclock.TimeClock t2 
WHERE t2.TcEmployeeID=TcEmployeeID 
AND t2.TcDate=TcDate 
AND TcDate IN (
  SELECT TcDate 
  from tclock.TimeClock 
  WHERE TcDate BETWEEN DATEADD(wk,DATEDIFF(wk,7, GETDATE()),-1) AND DATEADD(wk,DATEDIFF(wk,7, GETDATE()),5)
) 
GROUP BY TcEmployeeIDcode 

我得到的结果如下。但是,如何使用查询结果更新WeeklytotalHours列?

EmployeeId-----LastClockInDate-----WeeklyTotalhr           

      92000    2015-07-10       48.500000
      88000    2015-07-08       25.140000
      93000    2015-07-11       7.300000 

0 个答案:

没有答案