今天遇到的一个问题,
表结构:
optionID int identity(1,1) constraint o_pk primary key
account int ,
option char(1) check(option='d' or option='w' ) //w为取款 D为存款
quantity money
optionID option account quantity
1 d 2012 100
2 w 2013 200
1 w 2012 50
问题:统计各帐号的余额.
select account ,sum(
case quantity
when option='d' then quantity,
when option='w' then o-quantity
) as Balance
from accounttable
group by account
本文介绍了一个SQL查询案例,通过使用CASE WHEN语句结合GROUP BY来计算不同账户的余额。该方法适用于处理存款和取款记录,以得出每个账户的最新余额。
1107

被折叠的 条评论
为什么被折叠?



