1

列の合計2つの未払い残高を知りたいテーブルが1つあります

テーブルはこんな感じ

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の一意の行は取得されません。助けてください!!!

4

0 に答える 0