简体   繁体   中英

update values of sql table using the values from the same table

i have a table in sql server.

id    start    end     value    flag
 1      10     20.2    102       T
 2      22     11      133       T
 3      12.1   15      pending    F
 4      10     20.2    pending    F
 5      22     11      pending    F

as you can see, start and end is same for row 1 and 4 , and row 2 and 5 . But value of row with id 1 is 102, and that of 4 is pending ,

i want to write a query, which looks for another occurance of same start and end and then update the column value to the value of respective start and end, if it is pending. else leave it

ie I want to update the value column of the rows with id 4 and 5, with values in row with id 1 and 2.(pending to the respective value)

final result i am looking for is this:

id    start    end     value    flag
 1      10     20.2    102       T
 2      22     11      133       T
 3      12.1   15      pending    F
 4      10     20.2    102       F
 5      22     11      133       F

This answers the first version of the question, which I understood.

update t
    set status = 'done'
    where status = 'pending' and
          exists (select 1
                  from t t2
                  where t2.start = t.start and t2.end = t.end and
                        t2.id <> t.id
                 );

Actually, I assume you want at least one row to be 'done' (your question is not clear. If so, then add t2.status = 'done' to the subquery.

Or, in SQL Server, you can use an updatable CTE:

with toupdate as (
      select t.*, count(*) over (partition by start, end) as cnt
      from t
     )
update toupdate
    set status = 'done'
    where status <> 'done' and cnt > 1;

In this case, you would use sum(case when status = 'done' then 1 else 0 end) over (partition by start, end) as cnt instead of count(*) .

You can try a query like below

update t1
set t1.value=t2.value
from tbl t1 
join
tbl t2 on 
t1.start=t2.start and t1.end=t2.end and t2.value not like 'pending'
and t1.value like 'pending'

For you comment

if i havd to update Flag column also then?

please try this version

update t1
set t1.value=t2.value,
t1.flag=t2.flag
from tbl t1 
join
tbl t2 on 
t1.start=t2.start and t1.end=t2.end and t2.value not like 'pending'
and t1.value like 'pending'

Just I'm modifying Gordon Query as that's is the best answer you can get for the question

Query 1 :

 update t
        set status =

t.value >> Remove 'Done'

 where status = 'pending' and
          exists (select 1
                  from t t2
                  where t2.start = t.start and t2.end = t.end and
                        t2.id <> t.id
                 );

Query 2 :

 with toupdate as (
          select t.*, count(*) over (partition by start, end) as cnt
          from t
         )
    update T
        set status =

T.Value >> Remove 'Done'

 From toupdate
        where status <> 'done' and cnt > 1;

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