根据某个组更新特定记录

时间:2014-02-17 06:16:04

标签: sql-server-2008 sql-server-2008-r2

我正在使用SQL Server 2008 R2

我有一个数据库表,其中包含一些用户数据,如下所示:

Id  UserId  Sys     Dia     ReadingType     DataId              IsDeleted
1   10      98      65      last            1390556024216       0
2   10      99      69      average         1390556024216       0
3   10      102     96      last            1390562788540       0
4   10      102     96      average         1390562788540       0
5   11      130     98      last            1390631241547       0
6   11      130     98      average         1390631241547       0
7   2       285     199     first           1390770562374       0
8   2       250     180     last            1390770562374       0
9   2       267     189     average         1390770562374       0
10  1       258     180     first           1391191009457       0
11  1       258     180     last            1391191009457       0
12  1       258     180     average         1391191009457       0
13  1       285     199     additional      1391191009457       0
14  22      110     78      last            1391549208338       0
15  22      123     83      last            1391549208349       0

在此表中,有些记录包含same DataIddifferent ReadingType

我想为IsDeleted=1的记录设置ReadingType='last',并为ReadingType='average'设置same DataId, Sys, Dia and UserId的记录。

所以期望的结果应该是:

Id  UserId  Sys     Dia     Reading         DataId              IsDeleted
1   10      98      65      last            1390556024216       0
2   10      99      69      average         1390556024216       0
3   10      102     96      last            1390562788540       1
4   10      102     96      average         1390562788540       0
5   11      130     98      last            1390631241547       1
6   11      130     98      average         1390631241547       0
7   2       285     199     first           1390770562374       0
8   2       250     180     last            1390770562374       0
9   2       267     189     average         1390770562374       0
10  1       258     180     first           1391191009457       0
11  1       258     180     last            1391191009457       1
12  1       258     180     average         1391191009457       0
13  1       285     199     additional      1391191009457       0
14  22      110     78      last            1391549208338       0
15  22      123     83      last            1391549208349       0

此处Id 3, 5 and 11的记录应为marked as deleted,因为它们是having same UserId, Sys, Dia, DataId and ReadingType="last" with another record having ReadingType="average" with same other fields

有人可以帮我找到这样的记录并更新吗?

2 个答案:

答案 0 :(得分:1)

使用UPDATE和EXISTS子查询:

UPDATE T   
  SET IsDeleted=1
      WHERE
           ReadingType='last'  
           AND 
           EXISTS(SELECT * FROM T as T1 
                    WHERE T1.ReadingType='average'
                          AND T1.DataId=T.DataId
                          AND T1.Sys=T.Sys
                          AND T1.Dia=T.Dia
                          AND T1.UserId=T.UserId
                   )

SQLFiddle demo

答案 1 :(得分:-1)

您可以通过多种方式解决,但我在这里使用子查询来解决您的问题

UPDATE TABLE SET IsDeleted=1
       WHERE DataId=(SELECT DataId FROM TABEL WHERE Reading='last')