简体   繁体   中英

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 .

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.

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. Each ID_Grading can have up to 120 rows, each with a different ID_ObKind .

I think this will work for you. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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