pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ranieri Mazili <ranieri.oliveira@terra.com.br>
To: pgsql-general@postgresql.org
To: pgsql-sql@postgresql.org
Subject: Setting Variable - (Correct)
Date: Mon, 18 Jun 2007 11:34:21 -0300
Message-ID: <467697ED.8030506@terra.com.br> (raw)

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.
What's the correct form to concatenate strings with query in my case?

Thanks



view thread (7+ messages)  latest in thread

Message-ID: <467697ED.8030506@terra.com.br>
Permalink:  ../467697ED.8030506@terra.com.br/
Also on:    postgresql.org/message-id/467697ED.8030506@terra.com.br

 · 

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: ranieri.oliveira@terra.com.br, pgsql-general@postgresql.org
  Subject: Re: Setting Variable - (Correct)
  In-Reply-To: <467697ED.8030506@terra.com.br>

* 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