pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedHowto automatically define collumn names for a function result.
2+ messages / 2 participants
[nested] [flat]
* Howto automatically define collumn names for a function result.
@ 2009-08-27 11:08 Andreas <maps.on@gmx.net>
2009-08-27 11:25 ` Re: Howto automatically define collumn names for a function result. Jorge Godoy <jgodoy@gmail.com>
0 siblings, 1 reply; 2+ messages in thread
From: Andreas @ 2009-08-27 11:08 UTC (permalink / raw)
To: pgsql-sql
Hi,
wouldn't it be great to have functions return "setof something" as
result where "something" was determined out of the result of a SELECT
within the function?
like
CREATE OR REPLACE FUNCTION coffee(integer, timestamp, timestamp)
RETURNS SETOF
AS
$BODY$
SELECT staff_id, name, room, COUNT(coffee_id) AS cupcount
FROM staff JOIN coffee_log ON staff_fk = staff_id
WHERE (staff_id = $1) AND (coffee_time BETWEEN $2 AND $3)
GROUP BY staff_id, name, room
ORDER BY name;
$BODY$
LANGUAGE 'sql' STABLE
There the SELECT dumps a constant set of collumns where as far as I know
have to be defined as a type to make SETOF happy or define the names
whenever I call the function which would be tedious.
Actually this is a pretty simple example of some reports I need to produce.
They have around 60 collumns and there is also an aggregate and
filtering on an id as well as 2 timestamps.
Since the aggregate depends on id and timestamps too, it is no solution
to build a view and select from that within the function.
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Howto automatically define collumn names for a function result.
2009-08-27 11:08 Howto automatically define collumn names for a function result. Andreas <maps.on@gmx.net>
@ 2009-08-27 11:25 ` Jorge Godoy <jgodoy@gmail.com>
0 siblings, 0 replies; 2+ messages in thread
From: Jorge Godoy @ 2009-08-27 11:25 UTC (permalink / raw)
To: Andreas <maps.on@gmx.net>; +Cc: pgsql-sql
Have you tried returning SETOF RECORD[] and using the OUT specification?
CREATE OR REPLACE FUNCTION bla(integer, date, OUT date, OUT integer)
RETURNS SETOF RECORD[] AS
$_$
SELECT date AS output_date, $1+2 AS next_record FROM table WHERE id = $1
AND start_date >= $2;
$_$ LANGUAGE SQL;
(Just an example code, I haven't tried it myself. I know it works for
plpgsql functions, not sure for sql functions).
Regards,
--
Jorge Godoy <jgodoy@gmail.com>
On Thu, Aug 27, 2009 at 08:08, Andreas <maps.on@gmx.net> wrote:
> Hi,
> wouldn't it be great to have functions return "setof something" as result
> where "something" was determined out of the result of a SELECT within the
> function?
> like
>
> CREATE OR REPLACE FUNCTION coffee(integer, timestamp, timestamp)
> RETURNS SETOF
> AS
> $BODY$
> SELECT staff_id, name, room, COUNT(coffee_id) AS cupcount
> FROM staff JOIN coffee_log ON staff_fk = staff_id
> WHERE (staff_id = $1) AND (coffee_time BETWEEN $2 AND $3)
> GROUP BY staff_id, name, room
> ORDER BY name;
> $BODY$
> LANGUAGE 'sql' STABLE
>
> There the SELECT dumps a constant set of collumns where as far as I know
> have to be defined as a type to make SETOF happy or define the names
> whenever I call the function which would be tedious.
>
> Actually this is a pretty simple example of some reports I need to produce.
> They have around 60 collumns and there is also an aggregate and filtering
> on an id as well as 2 timestamps.
> Since the aggregate depends on id and timestamps too, it is no solution to
> build a view and select from that within the function.
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2009-08-27 11:25 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-08-27 11:08 Howto automatically define collumn names for a function result. Andreas <maps.on@gmx.net>
2009-08-27 11:25 ` Jorge Godoy <jgodoy@gmail.com>
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