Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 4DC82632E18 for ; Thu, 27 Aug 2009 08:08:43 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 94213-08 for ; Thu, 27 Aug 2009 11:08:34 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by mail.postgresql.org (Postfix) with SMTP id A7F6C6326C5 for ; Thu, 27 Aug 2009 08:08:34 -0300 (ADT) Received: (qmail invoked by alias); 27 Aug 2009 11:08:31 -0000 Received: from i59F793B8.versanet.de (EHLO [192.168.1.113]) [89.247.147.184] by mail.gmx.net (mp042) with SMTP; 27 Aug 2009 13:08:31 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX1+ORBJ3GgqOtKHzJVWgHf55iO/Qn6m4pF0+PWR7D7 3fWlubW5KUQlHV Message-ID: <4A966932.6050602@gmx.net> Date: Thu, 27 Aug 2009 13:08:34 +0200 From: Andreas User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Howto automatically define collumn names for a function result. Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.57 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/135 X-Sequence-Number: 33044 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.