基于结合了列组合和安全性的myOtherTable中的Amount列的总和,更新myTable中的列myAmountColumn的最佳方法是什么,除非何时 myTable.portfolio ='ABC',我想将其加入myOtherTable.portfolio ='123'。
谢谢。
这是myTable中的预期结果,而myOtherTable中是示例数据:
myTable
Portfolio Security myAmountColumn
ABC A 1000 --> this is from
myOtherTable.Amount where Portfolio = '123'
DEF B 2000 --> this is from
myOtherTable.Amount where Portfolio = 'DEF'
myOtherTable
Portfolio Security Amount
123 A 1000
DEF B 2000
以下查询出现错误
信息8152,第16层,状态14,第704行
字符串或二进制数据将被截断。
该语句已终止。
update j
set myAmountColumn =
(
select sum
(
abs(convert(float,c2.Amount))
)
from myOtherTablec2
where c2.portfolio =
CASE WHEN j.portfolio = 'ABC' THEN '123'
ELSE j.portfolio
END
and c2.pta_security = j.tab_security
),
CommentsColumn = CommentsColumn + ' my comments'
from myTable j, myOtherTablec
where c.portfolio =
CASE WHEN j.portfolio = 'ABC' THEN '123'
ELSE j.portfolio
END
and c.pta_security = j.tab_security
答案 0 :(得分:0)
看起来这是您唯一缺少的部分:
WHERE myOtherTable.portfolio=CASE
WHEN myTable.portfolio = 'ABC' THEN '123'
ELSE myTable.portfolio
END
尽管我使用“ WHERE”,但这也可能在JOIN的ON子句中。