使用 SUM 的这个子查询有问题。该查询将汇总特定日期范围内的几条记录的利润列。我将这个总利润按附在记录上的帐号分组。使用一个帐号,它就可以正常工作:
select
[ACCOUNT ID],
(
select SUM(PROFIT)
from [Transaction Table]
where [ACCOUNT ID] in ('1001')
and [ACTIVITY DATE] in ('5/31/2012')
) as 'May Profit'
from
[Transaction Table]
where
[ACCOUNT ID] in ('1001')
group by
[ACCOUNT ID]
给出正确的结果:
| ACCOUNT ID | May Profit |
---------------------------
| 1001 | $61.97 |
麻烦来自多个帐号:
select
[ACCOUNT ID],
(
select SUM(PROFIT)
from [Transaction Table]
where [ACCOUNT ID] in ('1001','2001')
and [ACTIVITY DATE] in ('5/31/2012')
) as 'May Profit'
from
[Transaction Table]
where
[ACCOUNT ID] in ('1001','2001')
group by
[ACCOUNT ID]
给出不正确的结果:
| ACCOUNT ID | May Profit |
---------------------------
| 1001 | $127.34 |
| 2001 | $127.34 |
每条记录上的两个账户的“五月利润”都在合计。我需要每个帐号的个人利润总额。
有什么想法吗?提前致谢!