Note that there are some explanatory texts on larger screens.

plurals
  1. POColumn Sum and subquery gives errors
    primarykey
    data
    text
    <p>I have one table where I want to know the outstanding balance of two the sum of columns</p> <p><strong>The Table</strong> looks something like this</p> <pre><code>journalid | senderid | sourcenr | accountnr | debitamount | creditamount | trxdate </code></pre> <p><strong>SQL QUERY</strong></p> <pre><code> 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 &gt;= '2012-12-31' and j.trxdate &lt; '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)) &gt; 0 order by 1 </code></pre> <p>I get the following error - </p> <blockquote> <p>Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause).</p> </blockquote> <p>If I add the <code>sourcenr</code> field (as shown below) </p> <pre><code> 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 &gt;= '2012-12-31' and j.trxdate &lt; '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)) &gt; 0 order by 1 </code></pre> <p>Then I DON'T get the error, but I don't get <strong>unique rows</strong> for senderid <strong>PLEASE HELP</strong> !!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload