Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有where子句的SQL更新具有group by和having语句_Sql_Sql Server - Fatal编程技术网

带有where子句的SQL更新具有group by和having语句

带有where子句的SQL更新具有group by和having语句,sql,sql-server,Sql,Sql Server,我正在尝试更新与WHERE子句条件匹配的所有行上的导出列,而它正在更新所有行 这是我到目前为止所拥有的 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 (Employe

我正在尝试更新与WHERE子句条件匹配的所有行上的导出列,而它正在更新所有行

这是我到目前为止所拥有的

 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))

您可以使用
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
;

您可以使用
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
;
请尝试使用此更新:

Update temp set exported = 1 where temp.Employeeid = ( select temp.Employeeid from temp group by employeeid,Date,Job having Min(punchtype) = MAX(punchtype) );
请尝试使用此更新:

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