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

From negora
Subject SUM the result of a subquery.
Date
Msg-id 4C7F95DE.9020200@negora.com
Whole thread Raw
Responses Re: SUM the result of a subquery.  (Jayadevan M <Jayadevan.Maymala@ibsplc.com>)
List pgsql-sql
<font face="Verdana">Hello:<br /><br /> I've a question related to the combination of the SUM aggregate function and
subqueries.Is it possible to SUM the resulting rows of a subquery? I'm explaining why I need this... I've a query like
this:</font><fontface="Verdana"><br /></font><blockquote><font face="Verdana">SELECT i.id_item, </font><font
face="Verdana">i.price, </font><font face="Verdana">SUM (o.quantity), </font><font face="Verdana">ROUND (SUM
(o.quantity)* i.price, 2) AS cost<br /> FROM orders o<br /> JOIN items i ON i.id_item = o.id_</font><font
face="Verdana">item</font><br/><font face="Verdana">WHERE o.date_order BETWEEN '2010-01-01' AND '2010-01-31'<br />
GROUPBY i.id_</font><font face="Verdana">item</font><font face="Verdana">, i.</font><font
face="Verdana">price</font><fontface="Verdana">;</font></blockquote><font face="Verdana"><br /> This just groups items
ofseveral orders by item ID, sums the quantities, multiplies such amounts by the price per unit, and rounds the result
to2 decimals. Very easy. The cost calculation is performed using the sum of the quantities instead of doing it per
line</font><fontface="Verdana"> to "lose" as less decimals as possible, because a rounding is applied on every
multiplication.<br/><br /> Now I need to get the total of ALL that lines in a separate query. It'd be really simple to
dosomething like this:<br /></font><blockquote><font face="Verdana">SELECT </font><font face="Verdana">SUM
(</font><fontface="Verdana">ROUND (o.quantity * i.price, 2))<br /> FROM orders o</font><font face="Verdana"><br /> JOIN
itemsi ON i.id_item = o.id_</font><font face="Verdana">item</font><br /><font face="Verdana">WHERE o.date_order BETWEEN
'2010-01-01'AND '2010-01-31';</font><br /></blockquote><br /> This multiplies the quantity of every line by the price
perunit, and sums the costs one by one. Done...<br /><br /> However, I'm obliged by the client to get an EXACT total
withNO DIFFERENCE of decimals (even though lots of them are "lost" during the rounded multiplications). He wants a
totalwhich MATCHES with the MANUAL sum of the results of the first query. It means that I need to do the same kind of
groupingwhich I perform on the first query and then sum all them. Hence, the reason behind my need.<br /><br />
PostgreSQLdoesn't allow nested SUMs, so I tried something like this:<br /><blockquote><font face="Verdana">SELECT SUM
(<br/></font><blockquote><font face="Verdana">(SELECT i.id_item, </font><font face="Verdana"> i.price, </font><font
face="Verdana">SUM(o.quantity), </font><font face="Verdana">ROUND (SUM (o.quantity) * i.price, 2) AS cost</font><br
/><fontface="Verdana">FROM orders o</font><br /><font face="Verdana">JOIN items i ON i.id_item = o.id_</font><font
face="Verdana">item</font><br/><font face="Verdana">WHERE o.date_order BETWEEN '2010-01-01' AND '2010-01-31'</font><br
/><fontface="Verdana">GROUP BY i.id_</font><font face="Verdana">item</font><font face="Verdana">, i.</font><font
face="Verdana">price</font><fontface="Verdana">)<br /></font></blockquote><font
face="Verdana">);</font></blockquote><br/> No luck. Obviously SUM expects an expression, not a set of rows. Is there a
wayto perform a sum of the resulting rows?<br /><br /> Thank you a lot.<font face="Verdana"><br /></font> 

pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: polygon overlay
Next
From: negora
Date:
Subject: SUM the result of a subquery.