1
我新的SQL,我試圖找出如何總結幾個欄目,並用1或0更新現有列如果列的總和大於0 此外,確保空值分配一個0值。T-SQL查詢更新
如何做這樣的事情在T-SQL中使用以下?
if (select (coalesce(COL1, 0) +
coalesce(COL2 0) +
coalesce(COL3, 0) +
coalesce(COL4, 0) +
coalesce(COL5, 0) from SOMETABLE where X_CONDITION = 10) > 0)
update SOMETABLE set ABC_COLUMN = 1
else
set ABC_COLUMN = 0;
編輯:
目前表看起來有點像這樣:
x_condition | col1 | col2 |col3| col4| col5| abc_column
------------| -----| -----| ---| ----|-----|-----------
7 | 1 | | | | | 0 (this in incorrect)
7 | 0 | 0 | | 0 | | 0 (this is correct)
5 | 1 | 1 | | | | 0 (this is incorrect)
我想固定abc_column - 幾乎像一個標誌:
x_condition | col1 | col2 |col3| col4| col5| abc_column
------------| -----| -----| ---| ----|-----|-----------
7 | 1 | | | | | 1
7 | 0 | 0 | | 0 | | 0
5 | 1 | 1 | | | | 1
謝謝!
編輯你的問題,並提供樣本數據和所需reulsts。 –
@GordonLinoff我提供什麼,我正在尋找一個簡單的例子。謝謝! –