title: PostgreSQL: How to multiply two columns and display result in same query?
body: <p>I have a query that it's select statement is this:</p>
  
  <pre><code>select Greatest(p.price,0) as newprice, sum(q.qty) as qty
  from ....
  </code></pre>
  
  <p>it gives me:</p>
  
  <pre><code>     newprice qty
        10      1
        0       1
        100     2
        1       2
  </code></pre>
  
  <p>I want to multiply newprice  with qty to get:</p>
  
  <pre><code>    newprice  qty   result
        10      1      10
        0       1      0
        100     2     200
        1       2      2
  </code></pre>
  
  <p>When I try to do <code>select Greatest(p.price,0) as newprice, sum(q.qty) as qty, newprice * qty</code> it says </p>
  
  <pre><code>ERROR: column "newprice" does not exist
  </code></pre>
  
  <p>I don't really need this extra column.</p>
  
  <p>what i really want is : <code>SUM(Greatest(p.price,0) * SUM(q.qty))</code>  which should give the value <code>212</code> but It says <code>ERROR: aggregate function calls cannot be nested</code></p>
  
  <p>Basically all I need is to multiply two columns and sum the result.
  I know I can use CTE something similar to what is shown <a href="https://stackoverflow.com/questions/15437704/multiplying-two-columns-which-have-been-calculated-on-a-case-statement">here</a> but I'm wondering if there is an easier way with less code.</p>