Re: SUM the result of a subquery.

From: negora <negora(at)negora(dot)com>
To: Jayadevan M <Jayadevan(dot)Maymala(at)ibsplc(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SUM the result of a subquery.
Date: 2010-09-02 12:36:00
Message-ID: 4C7F9A30.2010908@negora.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<font face="Verdana">Wow, I had no idea about this kind of SELECT
expression. It works flawless!!! Thank you lots Jayadevan</font><font
face="Verdana"> :) .</font><br>
<font face="Verdana"><br>
</font><br>
On 02/09/10 14:28, Jayadevan M wrote:
<blockquote
cite="mid:OF39AE7609(dot)CC04F696-ON65257792(dot)00446E7D-65257792(dot)00448A26(at)ibsplc(dot)com"
type="cite">
<blockquote type="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>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 2.0 KB

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tim Schumacher 2010-09-03 09:40:18 Generating Rows from a date and a duration
Previous Message Jayadevan M 2010-09-02 12:28:34 Re: SUM the result of a subquery.