Re: SUM the result of a subquery.

From: Jayadevan M <Jayadevan(dot)Maymala(at)ibsplc(dot)com>
To: negora <negora(at)negora(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org, pgsql-sql-owner(at)postgresql(dot)org
Subject: Re: SUM the result of a subquery.
Date: 2010-09-02 12:28:34
Message-ID: OF39AE7609.CC04F696-ON65257792.00446E7D-65257792.00448A26@ibsplc.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> 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."

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message negora 2010-09-02 12:36:00 Re: SUM the result of a subquery.
Previous Message negora 2010-09-02 12:19:15 SUM the result of a subquery.