我有两个临时表。
在表中,@due_cte
我有一系列Customer_Ids
,其后payment
是代码中各个点收集的金额。
该@work
表有很多不同的列,包括hist_amt_due.
我想做的是@work
使用amt_due进行更新,@due_cte
但group/sum
所有这些都通过customer_ID
我的原始代码:
update @work
set hist_amt_due = sum(isnull(due_amt,0)
from @work w
join @due_cte d on w.pmt_customer_no = d.pmt_customer_no
group by w.pmt_customer_no
随后出现错误消息:
Incorrect syntax near the keyword 'group'.
经过一番挖掘后,我发现我应该使用内部联接,因此我尝试了此操作,但是这仍然无法正常工作。
update @work
set hist_amt_due = isnull(d.due_amt,0)
from @work w
inner join (SELECT pmt_customer_no, sum(isnull(due_amt,0)) from @due_cte group by pmt_customer_no) AS d on w.pmt_customer_no = d.pmt_customer_no
错误信息:
No column name was specified for column 2 of 'd'.
和
Invalid column due_amount
我对如何使这一点迷茫不知所措。
样本数据:
pmt_customer_no | due_amt
1 50
2 30
3 0
4 30
2 10
1 20
5 80
@work
应该更新-在客户号为1的due_amout
应该是:70,在它的2则due_amount
应该是40,等等。
尝试这个:
update w set hist_amt_due = d.s
from @work w
join (select pmt_customer_no, sum(isnull(due_amt, 0)) s
from @due_cte group by pmt_customer_no)d on w.pmt_customer_no = d.pmt_customer_no
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句