Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id C1C1A9FBAA9; Mon, 18 Jun 2007 13:15:26 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 90909-04; Mon, 18 Jun 2007 13:15:14 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mtao04.charter.net (mtao04.charter.net [209.225.8.178]) by postgresql.org (Postfix) with ESMTP id C97EB9FB87F; Mon, 18 Jun 2007 13:15:14 -0300 (ADT) Received: from aa02.charter.net ([10.20.200.154]) by mtao04.charter.net (InterMail vM.7.08.02.00 201-2186-121-20061213) with ESMTP id <20070618161514.ECDZ22186.mtao04.charter.net@aa02.charter.net>; Mon, 18 Jun 2007 12:15:14 -0400 Received: from [10.0.1.2] (really [66.188.164.45]) by aa02.charter.net with ESMTP id <20070618161514.MPS15491.aa02.charter.net@[10.0.1.2]>; Mon, 18 Jun 2007 12:15:14 -0400 In-Reply-To: <108B61B7-DB9A-4FE3-832C-323866956DFE@seespotcode.net> References: <467697ED.8030506@terra.com.br> <108B61B7-DB9A-4FE3-832C-323866956DFE@seespotcode.net> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <7DA5333D-3437-4033-A222-128E541A04C2@seespotcode.net> Cc: Ranieri Mazili , pgsql-general@postgresql.org, pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: Setting Variable - (Correct) Date: Mon, 18 Jun 2007 11:15:12 -0500 To: Michael Glaesemann X-Mailer: Apple Mail (2.752.2) X-Chzlrs: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/1059 X-Sequence-Number: 115882 On Jun 18, 2007, at 10:17 , Michael Glaesemann wrote: > Looking over your function, I'm a little confused about what you're > trying to do. I'm guessing the (final) result you're trying to get > is the number of hours lost for each product per area per month for > the three years prior to the provided date. Or, rather, the number of hours lost per production area per month for the three years prior to the provided date. > SELECT date_trunc('month', production.production_date)::date > AS production_period > , product.id_production_area > , sum(production_hours) as total_production_hours > FROM production > JOIN product USING (id_product) > WHERE lost_hours = 'S' > AND date_trunc('month', a.production_date)::date BETWEEN > date_trunc('month', ? - 3 * interval '1 year')::date > AND date_trunc('month', ?)::date; Looks like I forgot the GROUP BY clause: GROUP BY production_period, id_production_area Michael Glaesemann grzm seespotcode net