TSQL - 使用 ORDER BY 更新列值直到找到某个列值

TSQL - Updating a column value with ORDER BY till a certain column value is found

我有这个table:

ID - Uniqueidentifier
TYPEDUT_ID - Uniqueidentifer
OPERATION - Integer
ACTIVE - Integer (0/1)
CREATED - DateTime

我想对行 ORDER BY CREATED DESC 进行排序并从上到下更新 ACTIVE = 0 直到达到某个操作。

示例数据:

            ID                                     TYPEDUT_ID                 OPERATION        ACTIVE             CREATED
ADFFC7CB-C938-448B-BF15-019C31434AFD    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2000             1         2016-09-06 13:06:29.333
CF7E2375-EC6E-416F-99E6-15213BD829D8    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    3000             1         2016-09-06 13:01:47.657
31189043-8B7E-4DCC-8EAC-62AD5EDB32E2    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2000             1         2016-09-06 13:10:07.720
E473B887-65DB-40FA-94D9-697F20E787B9    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2300             1         2016-09-06 13:30:41.227
1DD2C120-7859-4868-9C71-83F6BC3F7488    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    3100             1         2016-09-06 14:40:46.087
B27283A8-43DD-468E-95CB-99DD1BC95321    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2100             1         2016-09-06 13:23:34.883
103899E9-33B1-4FCA-AA1B-A51040B35FBD    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2400             1         2016-09-06 14:03:21.830

现在,如果我将 3000 作为我想要的 OPERATION,预期结果应该如下所示:

                   ID                              TYPEDUT_ID                 OPERATION  ACTIVE           CREATED
1DD2C120-7859-4868-9C71-83F6BC3F7488    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    3100       0       2016-09-06 14:40:46.087
103899E9-33B1-4FCA-AA1B-A51040B35FBD    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2400       0       2016-09-06 14:03:21.830
E473B887-65DB-40FA-94D9-697F20E787B9    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2300       0       2016-09-06 13:30:41.227
B27283A8-43DD-468E-95CB-99DD1BC95321    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2100       0       2016-09-06 13:23:34.883
31189043-8B7E-4DCC-8EAC-62AD5EDB32E2    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2000       0       2016-09-06 13:10:07.720
ADFFC7CB-C938-448B-BF15-019C31434AFD    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    2000       0       2016-09-06 13:06:29.333
CF7E2375-EC6E-416F-99E6-15213BD829D8    CC055EBE-2655-4B2F-B6C2-BF9BCE46941B    3000       1       2016-09-06 13:01:47.657

如果我没理解错的话,您可以使用子查询获取特定操作的最小创建时间。然后您可以使用该信息来定义要更新的行:

update t
    set active = 0
    where created <= (select min(t2.created)
                      from t t2
                      where operation = @Operation
                     );

编辑:

看来,我理解反了。反之解决方法:

update t
    set active = 0
    where created >= (select max(t2.created)
                      from t t2
                      where operation = @Operation
                     );

由于需要按照Created的降序对数据进行排序。我使用了一个临时的 table 来存储排序后的数据。

 DECLARE @tempTable1 TABLE(Temp_Id INT IDENTITY(1,1),Id NVARCHAR(MAX),TYPEDUT_ID  NVARCHAR(MAX),OPERATION INT,ACTIVE INT,CREATED DATETIME)

    DECLARE @operation INT =3000
    INSERT INTO  @tempTable1(Id,TYPEDUT_ID,OPERATION,ACTIVE,CREATED) SELECT Id,TYPEDUT_ID,OPERATION,ACTIVE,CREATED FROM Table_Name ORDER BY CREATED DESC

    UPDATE @tempTable1
    SET active = 0
    WHERE operation != @operation

    SELECT * FROM @tempTable1