Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id D49179FB639; Mon, 18 Jun 2007 12:18:03 -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 35253-01; Mon, 18 Jun 2007 12:17:57 -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 BD7E19FB5ED; Mon, 18 Jun 2007 12:17:56 -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 <20070618151751.TTZN1527.mtao04.charter.net@aa02.charter.net>; Mon, 18 Jun 2007 11:17:51 -0400 Received: from [10.0.1.2] (really [66.188.164.45]) by aa02.charter.net with ESMTP id <20070618151751.TTXE15491.aa02.charter.net@[10.0.1.2]>; Mon, 18 Jun 2007 11:17:51 -0400 In-Reply-To: <467697ED.8030506@terra.com.br> References: <467697ED.8030506@terra.com.br> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <108B61B7-DB9A-4FE3-832C-323866956DFE@seespotcode.net> Cc: 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 10:17:45 -0500 To: Ranieri Mazili X-Mailer: Apple Mail (2.752.2) X-Chzlrs: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/1049 X-Sequence-Number: 115872 On Jun 18, 2007, at 9:34 , Ranieri Mazili wrote: > Hello, I'm trying do the following function: > > CREATE OR REPLACE FUNCTION lost_hours_temp(date) > RETURNS text AS > $BODY$ > DECLARE > START_DATE date; > END_DATE date; > QUERY text; > BEGIN > START_DATE := $1; > END_DATE := START_DATE - interval '3 year'; > WHILE EXTRACT(YEAR FROM START_DATE) = EXTRACT(YEAR FROM > END_DATE)+3 LOOP > QUERY := 'SELECT CAST(EXTRACT(YEAR FROM > A.production_date) || '/' || EXTRACT(MONTH FROM A.production_date) > AS TEXT) as date, > SUM(production_hours) AS production_hours, > B.id_production_area > FROM production A, product B > WHERE EXTRACT(MONTH FROM production_date) = EXTRACT > (MONTH FROM ' || START_DATE || ') > AND EXTRACT(YEAR FROM A.production_date) = EXTRACT > (YEAR FROM ' || START_DATE || ') > AND lost_hours = ' || 'S' ||' > AND A.id_product = B.id_product > GROUP BY id_production_area, date'; > START_DATE := START_DATE - interval '1 month'; > END LOOP; > > RETURN QUERY; > END; > $BODY$ > LANGUAGE 'plpgsql'; > > My problem is into WHILE, I'm trying to concatenate variables with > the string, but I guess that it's generating an error. It looks like you've got a number of problems here, but overall it appears you're approaching this from the wrong way. What's the final result you want? I doubt it's just a query string. You probably want to run this query somewhere, and you can do this from within PL/ pgSQL. You may want to look up set returning functions. 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. You should be able to do this in just a single SQL query, something like: 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; Things that were puzzling to me about your code: * START_DATE is *after* END_DATE (END_DATE := START_DATE - INTERVAL '3 year') * WHILE EXTRACT(YEAR FROM START_DATE) = EXTRACT(YEAR FROM END_DATE)+3 LOOP will only be true for a limited number of months, not over the whole three-year range. The idea of three years has no real meaning in the query after this point. Anyway, hope this helps. Michael Glaesemann grzm seespotcode net