简体   繁体   English

SQL Server 2005更新查询基于同一表

[英]SQL Server 2005 update query based on same table

I have a table with the following fields: ID_Observation (PK), ID_Grading , ID_ObKind , Data . 我有一个包含以下字段的表: ID_Observation (PK), ID_GradingID_ObKindData

I'm having difficulty writing a query to update some rows based on other rows within the table. 我在编写查询以根据表中的其他行更新某些行时遇到困难。

If ID_ObKind = 9 AND Data = 'No' then any row which has the same ID_Grading with ID_ObKind = 10 and Data = NULL needs to be updated to Data = 0. 如果ID_ObKind = 9 AND Data ='No',那么具有相同ID_Grading且ID_ObKind = 10和Data = NULL的任何行都需要更新为Data = 0。

I have about a dozen different cases where data needs to be changed depending on the value of Data for a specific ID_ObKind so need to nail this down. 我有大约十二种不同的情况,其中需要根据特定ID_ObKind的Data值来更改数据,因此需要对此进行详细说明。 Each ID_Grading can have up to 120 rows, each with a different ID_ObKind . 每个ID_Grading最多可包含120行,每行具有不同的ID_ObKind

I think this will work for you. 我认为这对您有用。 http://sqlfiddle.com/#!3/2eeaf/14 http://sqlfiddle.com/#!3/2eeaf/14

Update TheTable
   set data=0
 where id_grading = (select id_grading
                       from TheTable
                      where id_obkind=9
                        and data='No')
   and id_obkind=10
   and data is null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM