Re: SUM the result of a subquery. - Mailing list pgsql-sql

From negora
Subject Re: SUM the result of a subquery.
Date
Msg-id 4C7F9A30.2010908@negora.com
Whole thread Raw
In response to Re: SUM the result of a subquery.  (Jayadevan M <Jayadevan.Maymala@ibsplc.com>)
List pgsql-sql
<font face="Verdana">Wow, I had no idea about this kind of SELECT expression. It works flawless!!! Thank you lots
Jayadevan</font><fontface="Verdana"> :) .</font><br /><font face="Verdana"><br /></font><br /> On 02/09/10 14:28,
JayadevanM wrote: <blockquote cite="mid:OF39AE7609.CC04F696-ON65257792.00446E7D-65257792.00448A26@ibsplc.com"
type="cite"><blockquotetype="cite"><pre wrap="">SELECT SUM (
 
(SELECT i.id_item, i.price, SUM (o.quantity), ROUND (SUM 
(o.quantity) * i.price, 2) AS cost
FROM orders o
JOIN items i ON i.id_item = o.id_item
WHERE o.date_order BETWEEN '2010-01-01' AND '2010-01-31'
GROUP BY i.id_item, i.price)
); 

No luck. Obviously SUM expects an expression, not a set of rows. Is 
there a way to perform a sum of the resulting rows?
   </pre></blockquote><pre wrap="">I don't have a PostgreSQL server to try this right now. But you are 
looking for something like 
SELECT SUM (cost) from (
(SELECT i.id_item, i.price, SUM (o.quantity), ROUND (SUM (o.quantity) * 
i.price, 2) AS cost
FROM orders o
JOIN items i ON i.id_item = o.id_item
WHERE o.date_order BETWEEN '2010-01-01' AND '2010-01-31'
GROUP BY i.id_item, i.price)
) as x

Regards,
Jayadevan





DISCLAIMER: 

"The information in this e-mail and any attachment is intended only for 
the person to whom it is addressed and may contain confidential and/or 
privileged material. If you have received this e-mail in error, kindly 
contact the sender and destroy all copies of the original communication. 
IBS makes no warranty, express or implied, nor guarantees the accuracy, 
adequacy or completeness of the information contained in this email or any 
attachment and is not liable for any errors, defects, omissions, viruses 
or for resultant loss or damage, if any, direct or indirect."





 </pre></blockquote>

pgsql-sql by date:

Previous
From: Jayadevan M
Date:
Subject: Re: SUM the result of a subquery.
Next
From: Tim Schumacher
Date:
Subject: Generating Rows from a date and a duration