SQL Server选择和更新查询问题

时间:2015-06-30 12:49:13

标签: sql sql-server sql-server-2012

问题

我正在尝试创建一个将在SQL Server Agent Daily上运行的UPDATE查询...但是我无法从表中选择正确的值..

表:

CREATE TABLE Staff (StaffID INT IDENTITY (1,1) NOT NULL,
Name VARCHAR(70))

CREATE TABLE EntryLine (EntryID INT IDENTITY (1,1) NOT NULL,
StaffID INT,
EntryDetails VARCHAR(500),
EntryDate DATETIME,
Count INT)

CREATE TABLE Timesheet (TimesheetID INT IDENTITY (1,1) NOT NULL,
StaffID INT
StartDate DATETIME,
EndDate DATETIME)

示例数据

StaffID Name        
   1    John        
   2    Ian     

EntryID StaffID EntryDetails    EntryDate          count 
   1       1       test1    28/06/2015 07:58:06      1
   2       1       test2    28/06/2015 12:03:47      2
   3       1       test3    28/06/2015 13:01:12      3
   4       1       test4    28/06/2015 17:34:56      4
   5       2       test5    28/06/2015 07:48:24      1
   6       2       test6    28/06/2015 17:31:42      2

TimesheetID StaffID      StartDate            EndDate
    1          1    28/06/2015 07:58:06        NULL
    2          2    28/06/2015 07:48:24        NULL

如您所见,我已将EndDate设置为当前时间表中的NULL。现在应该发生的是查询将EndDate更新为表EntryLine中当天的最后一个条目,因此最终数据将为:

TimesheetID StaffID       StartDate                 EndDate
     1         1     28/06/2015 07:58:06       28/06/2015 17:34:56
     2         2     28/06/2015 07:48:24       28/06/2015 17:31:42

我已经有一个计数器列(Count),它计算每个员工每天的条目数,所以我不知道我是应该利用这个还是在当天的最新日期基于UPDATE查询。

请有人可以告诉我如何使用此查询来更新我的表格:

  • EndDate IS NULL
  • StaffID匹配
  • EndDate是同一天作为StartDate

    select
    EntryLine.StaffID,
    COUNT(count)FieldCounter,
    MAX(EntryLine.EntryDate)EntryDate,
    dateadd(DAY,0, datediff(day,0, EntryLine.EntryDate))GenericDate
    
    from EntryLine
    GROUP BY EntryLine.StaffID, dateadd(DAY,0, datediff(day,0, EntryLine.EntryDate))
    

3 个答案:

答案 0 :(得分:3)

您可以为UPDATE使用相关子查询:

SQL Fiddle

UPDATE t
    SET t.EndDate = (
                    SELECT MAX(EntryDate) 
                    FROM EntryLine 
                    WHERE 
                        StaffID = t.StaffID 
                        AND CAST(EntryDate AS DATE) = CAST(t.StartDate AS DATE)
                    )
FROM Timesheet t
WHERE t.EndDate IS NULL

使用JOIN的另一种方法:

SQL Fiddle

UPDATE t
    SET t.EndDate = a.MaxEntryDate
FROM Timesheet t
INNER JOIN(
    SELECT 
        StaffId, 
        CAST(EntryDate AS DATE) AS EntryDate,
        MAX(EntryDate) AS MaxEntryDate
    FROM EntryLine
    GROUP BY StaffId, CAST(EntryDate AS DATE)
)a
    ON a.StaffID = t.StaffID
    AND CAST(t.StartDate AS DATE) = a.EntryDate
WHERE t.EndDate IS NULL

答案 1 :(得分:1)

您可以使用子查询。

     UPdate T 
        set T.EndDate = X.Exittime
        FROM Timesheet T JOIN (select StaffID,Max(EntryDate) as Exittime from EntryLine Group BY StaffID)X 
        ON X.StaffID = T.StaffID 
         AND CAST(t.StartDate AS DATE) = CAST(X.Exittime AS DATE)
where T.EndDate IS NULL

答案 2 :(得分:1)

You do not need to make other(third) table for the same data i.e. duplication of DATA,

just use this query to get the required result:

(Third Table you created is not necessary at all)

SELECT staffid,
       CONVERT(VARCHAR, entrydate, 102) AS ForDate,
       Min(entrydate)                   AS EntryTime,
       Max(entrydate)                   AS ExitTime
FROM   entryline
GROUP  BY staffid,
          CONVERT(VARCHAR, entrydate, 102) 

Hope this helps,