2017-04-24 99 views
0

我試圖更新在符合我的WHERE子句條件的所有行出口列,它是更新所有行,而不是SQL更新已具有聲明組和

這裏是我到目前爲止

Create table Temp (
    Employeeid nvarchar(10), 
    Punchtype nvarchar (2), 
    Date Date, 
    TotalTime decimal (16,2), 
    Job nvarchar(10), 
    Exported bit not null Default 0) 

    Insert Into Temp (Employeeid,Punchtype,Date,TotalTime,Job) 
    Values ('1234','C','4/24/2017',4,'J1234'), ('1234','RW','4/24/2017',4,'J1234'),('4563','C','4/24/2017',2,'J1234'), ('4563','C','4/24/2017',5,'J1234') 

    Update temp set exported = 1 where exists (select null from temp group by employeeid,Date,Job having Min(punchtype) = MAX(punchtype)) 

回答

2

您可以使用CTE和窗口功能:

WITH CTE AS 
(
    SELECT *, 
      MIN(punchtype) 
      OVER(PARTITION BY employeeid, [Date], Job) Min_punchtype, 
      MAX(punchtype) 
      OVER(PARTITION BY employeeid, [Date], Job) Max_punchtype 
    FROM Temp 
) 
UPDATE CTE 
SET exported = 1 
WHERE Min_punchtype = Max_punchtype 
; 
0

嘗試使用此更新:

Update temp set exported = 1 where temp.Employeeid = (select temp.Employeeid from temp group by employeeid,Date,Job having Min(punchtype) = MAX(punchtype));