1

Here is a simplified table structre for what I am trying to do:

T1:
id,uid,group,value1
1,1,A,3
2,1,B,4
3,2,A,6
4,2,B,7

now I want that any uid that has both a row for group A and group B the value assosicated with group B will be added to the value in the row associated with Group B.

so the result for the above would be
1,1,A,7 (3+4)
2,1,B,4
3,2,A,13 (6+7)
4,2,B,7

there might also be uids for which only group A exists or only groups B exists in which case no changes t those lines should take place.

What I did so far is that I created a query that has one row for any row that needs to be updated:

id,uid,newval
1,1,7
3,2,13

The subquery is somewhat complex and joins the table that needs to be updated with it self.

now how do I use this to update the original table? (or how do I do it some other way)?

thanks.

1 Answer 1

1
Update T1
set Value1 = T1.Value + T2.Value
from T1 inner join T1 as T2 on T1.uid = T2.uid
where T1.group = "A" and T2.group = "B"
Sign up to request clarification or add additional context in comments.

2 Comments

how would this go If I want an T1 to be the alias for MyTableWithLongName ? and what If I need to update two fields at once ?
Update MyTableWithLongName set Value1 = T1.Value + T2.Value, Value2 = x from MyTableWithLongName as T1 inner join MyTableWithLongName as T2 on T1.uid = T2.uidwhere T1.group = "A" and T2.group = "B"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.