我有一张表,我想知道两个列总和的未结余额
表看起来像这样
journalid | senderid | sourcenr | accountnr | debitamount | creditamount | trxdate
SQL查询
select j.senderid,
(sum(j.debitamount)-(select sum(j2.creditamount)as balance from
journal j2 where j2.sourcenr = j.sourcenr and j2.ccaountnr =
3993200))as outstanding
from journal j
where j.accountnr = 3993200
and( j.trxdate >= '2012-12-31' and j.trxdate < '2013-01-31')
group by 1
having (sum(j.debitamount)-(select sum(j2.creditamount)as balance from
journal j2 where j2.source = j.source and j2.accnr = 3993200)) > 0
order by 1
我收到以下错误 -
选择列表中的无效表达式(不包含在聚合函数或 GROUP BY 子句中)。
如果我添加sourcenr
字段(如下图)
select j.senderid,
j.sourcenr,
(sum(j.debitamount)-(select sum(j2.creditamount)as balance from
journal j2 where j2.sourcenr = j.sourcenr and j2.ccaountnr =
3993200))as outstanding
from journal j
where j.accountnr = 3993200
and( j.trxdate >= '2012-12-31' and j.trxdate < '2013-01-31')
group by 1 ,2
having (sum(j.debitamount)-(select sum(j2.creditamount)as balance from
journal j2 where j2.source = j.source and j2.accnr = 3993200)) > 0
order by 1
然后我没有得到错误,但我没有得到senderid的唯一行请帮助!