我在帐户表格中有一个名为accounts
的表格,其列为:
-account names
-account status
-balance
-account grp
帐户状态仅包括当前或过期...我已设法选择不同的帐户Grp以及属于特定帐户组的每个帐户名称的所有余额总和...我的问题我还希望获得帐户的总余额与当前和过去的帐户状态,因为添加后将导致总余额
就像下面的输出
Account Status
Account Grp | Total Balance | Current | Past Due |
1001 | 500 | 250 | 250 |
1003 | 150 | 100 | 50 |
1003 | 100 | 25 | 75 |
答案 0 :(得分:2)
试试这个
SELECT [account grp],
[account names],
Sum(balance) [Total balance],
Sum(CASE [account status]
WHEN 'Current' THEN balance
ELSE 0.00
END) CURRENT,
Sum(CASE [account status]
WHEN 'Past' THEN balance
ELSE 0.00
END) Past
FROM [accounts]
GROUP BY [account grp],
[account names]