SQL Syntax Help - Update
I have some code like this:
Update table_name
set
[column] = case when d.data is null, then null else d.columnname end.
from...
etc
My question is, how do I 开发者_高级运维set up a function where the 'else d.columnname' is to sum up several columns from joins.
Would it be something like:
...then null else sum(d.column1 + rf.column2 + rwf.column3) as tempcolumn end,
or
...then null else (d.column1 + rf.column2 + rwf.column3) end,
What is the correct way to do a column sum in this set situation?
You can simply do:
update MyTable
set column =
case
when d.data is not null
then d.column1 + rf.column2 + rwf.column3
end
from ...
CASE
will return NULL
by default when there is no match.
Something like this should work:
UPDATE table_name
SET [column] = CASE WHEN d.data IS NULL
THEN null
ELSE (d.column1 + rf.column2 + rwf.column3)
END
FROM table_name
INNER JOIN other_table1 d ON ...
INNER JOIN other_table2 rf ON ...
INNER JOIN other_table3 rwf ON ...
Of course, in the query above you have to put in the correct relations between the tables in the INNER JOIN - ON clauses
精彩评论