0

我有下表,我需要将 Check_Status 从 Completed 更新为“Not Completed”以获取特定 Prod_ID 的最大 Transaction_ID:

Tran_ID     Prod_ID    Check_Status
-------     --------   ------------
107893      XA         Completed
107892      XA         Completed
107891      XA         Completed
107890      XB         Completed
107889      XB         Completed
107888      YC         Completed
107887      YC         Completed
107886      YC         Completed
107885      YC         Completed

我的结果应显示如下,即产品 IDXA的最大 Trans Id 为 107893,应更新为“未完成”。其余的东西应该不变。对于其他产品 IDXBXC.

Tran_ID    Prod_ID     Check_Status
-------    --------    ------------
107893     XA          Not-Completed
107892     XA          Completed
107891     XA          Completed
107890     XB          Not-Completed
107889     XB          Completed
107888     YC          Not-Completed
107887     YC          Completed
107886     YC          Completed
107885     YC          Completed
4

3 回答 3

3

您可以为此使用 CTE:

;with cte as
(
  select tran_id, prod_id,
    row_number() over(partition by prod_id order by tran_id desc) rn,
    check_status
  from yourtable
) 
update  cte
set check_status = 'Not-Completed'
where rn = 1

SQL Fiddle with Demo

于 2012-09-18T12:09:49.323 回答
1
     UPDATE Transactions set 
    Check_Status= 'Not completed' 
     where Tran_ID IN (
        Select MAX(Tran_ID) as Tran_ID from 
Transactions Group by Prod_ID)
于 2012-09-18T12:05:33.820 回答
1
UPDATE tablename t1 
SET t1.Check_Status = 'Not-Completed'
WHERE t1.TranId IN 
    (
       SELECT Max(Tran_ID) 
       FROM Tablename t2 
       GROUP BY Prod_ID
    )
于 2012-09-18T12:06:25.547 回答