[SQL] SUM the result of a subquery.
Hello: 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: 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; This just groups items of several orders by item ID, sums the quantities, multiplies such amounts by the price per unit, and rounds the result to 2 decimals. Very easy. The cost calculation is performed using the sum of the quantities instead of doing it per line to "lose" as less decimals as possible, because a rounding is applied on every multiplication. Now I need to get the total of ALL that lines in a separate query. It'd be really simple to do something like this: SELECT SUM (ROUND (o.quantity * i.price, 2)) 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'; This multiplies the quantity of every line by the price per unit, and sums the costs one by one. Done... However, I'm obliged by the client to get an EXACT total with NO DIFFERENCE of decimals (even though lots of them are "lost" during the rounded multiplications). He wants a total which MATCHES with the MANUAL sum of the results of the first query. It means that I need to do the same kind of grouping which I perform on the first query and then sum all them. Hence, the reason behind my need. PostgreSQL doesn't allow nested SUMs, so I tried something like this: 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? Thank you a lot.
[SQL] SUM the result of a subquery.
Hello: 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: 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; This just groups items of several orders by item ID, sums the quantities, multiplies such amounts by the price per unit, and rounds the result to 2 decimals. Very easy. The cost calculation is performed using the sum of the quantities instead of doing it per line to "lose" as less decimals as possible, because a rounding is applied on every multiplication. Now I need to get the total of ALL that lines in a separate query. It'd be really simple to do something like this: SELECT SUM (ROUND (o.quantity * i.price, 2)) 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'; This multiplies the quantity of every line by the price per unit, and sums the costs one by one. Done... However, I'm obliged by the client to get an EXACT total with NO DIFFERENCE of decimals (even though lots of them are "lost" during the rounded multiplications). He wants a total which MATCHES with the MANUAL sum of the results of the first query. It means that I need to do the same kind of grouping which I perform on the first query and then sum all them. Hence, the reason behind my need. PostgreSQL doesn't allow nested SUMs, so I tried something like this: 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? Thank you a lot.
Re: [SQL] SUM the result of a subquery.
> 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? > 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." -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql
Re: [SQL] SUM the result of a subquery.
Wow, I had no idea about this kind of SELECT _expression_. It works flawless!!! Thank you lots Jayadevan :) . On 02/09/10 14:28, Jayadevan M wrote: 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? 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."