I am trying to create an payment report which lists the sum in each account. The table is in SQL SERVER 2005 and it has the following table
我正在尝试创建一个付款报告,列出每个账户的金额。这个表位于SQL SERVER 2005中,它有如下表。
[Account] [Amount] [Type]
(账户)(金额)(类型)
1111 10 C
1111 10 C
1111 10 C
1111 10 C
1111 15 D
1111 15 D
1111 5 D
1111 5 D
1112 10 C
1112 10 C
1112 15 C
1112 15 C
1112 10 D
1112 10 D
1112 10 D
1112 10 D
I need to create report that will sum the Credit and Debit for each account Output
我需要创建一份报告,将每个帐户输出的贷方和借方相加
1111 0
1111年0
1112 5
1112年5
Is there a single SELECT statement that I can use to generate the output? I can do this by creating temp tables but I was wondering if I can do it in a single SELECT statement
是否有一个SELECT语句可以用来生成输出?我可以通过创建临时表来实现这一点,但是我想知道是否可以在一个SELECT语句中实现这一点
1 个解决方案
#1
4
select Account,
sum(case when Type='D' then Amount * -1 else Amount end) as AmountSum
from Payment
group by Account
#1
4
select Account,
sum(case when Type='D' then Amount * -1 else Amount end) as AmountSum
from Payment
group by Account