pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: negora <negora@negora.com>
To: Jayadevan M <Jayadevan.Maymala@ibsplc.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: SUM the result of a subquery.
Date: Thu, 02 Sep 2010 14:36:00 +0200
Message-ID: <4C7F9A30.2010908@negora.com> (raw)
In-Reply-To: <OF39AE7609.CC04F696-ON65257792.00446E7D-65257792.00448A26@ibsplc.com>
References: <4C7F95DE.9020200@negora.com>
	<OF39AE7609.CC04F696-ON65257792.00446E7D-65257792.00448A26@ibsplc.com>

<!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.CC04F696-ON65257792.00446E7D-65257792.00448A26@ibsplc.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>



view thread (4+ messages)

Message-ID: <4C7F9A30.2010908@negora.com>
Permalink:  ../4C7F9A30.2010908@negora.com/
Also on:    postgresql.org/message-id/4C7F9A30.2010908@negora.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: negora@negora.com, Jayadevan.Maymala@ibsplc.com
  Subject: Re: SUM the result of a subquery.
  In-Reply-To: <4C7F9A30.2010908@negora.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox