[SQL Server Management Studio 2016,TSQL]
你能帮我解决这个SQL吗?
我有以下行:
Account Reference | Balance
100291 -301
100291 157
102388 203
但是,我想将前两行的余额添加到一起,并按帐户参考分组,如下所示:
Account Reference | Balance
100291 -144
102388 203
供您参考,代码如下:
SELECT
RB.AccountReference,
RB.CurrentBalance
FROM SB_RentAccountBalances_V RB
LEFT JOIN RentAccounts RA ON RB.AccountReference = RA.ID
有人可以就如何做到这一点给我建议吗?
答案 0 :(得分:1)
SELECT
RB.AccountReference,
sum(isnull(RB.CurrentBalance,0))
FROM SB_RentAccountBalances_V RB
LEFT JOIN RentAccounts RA ON RB.AccountReference = RA.ID
group by
RB.AccountReference
答案 1 :(得分:0)
从您的示例中,不清楚是否需要JOIN
:
SELECT RB.AccountReference, SUM(RB.CurrentBalance) as CurrentBalance
FROM SB_RentAccountBalances_V RB
GROUP BY RB.AccountReference;