Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id B702C2E347D for ; Mon, 17 Dec 2007 09:07:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 93291-01-9 for ; Mon, 17 Dec 2007 09:07:29 -0400 (AST) Received: from sargon.lncsa.com (sargon.lncsa.com [212.99.8.251]) by postgresql.org (Postfix) with ESMTP id 750982E33D5 for ; Mon, 17 Dec 2007 09:06:32 -0400 (AST) Received: from localhost (localhost.localdomain [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id 73E9530A9024 for ; Mon, 17 Dec 2007 14:06:30 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at lncsa.com Received: from sargon.lncsa.com ([127.0.0.1]) by localhost (sargon.lncsa.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id V9lyxtdjIrWl for ; Mon, 17 Dec 2007 14:06:30 +0100 (CET) Received: from zenon.apartia.fr (zenon.apartia.fr [10.0.3.1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "zenon.apartia.fr", Issuer "ca.apartia.fr" (verified OK)) by sargon.lncsa.com (Postfix) with ESMTP id 467F130A7837 for ; Mon, 17 Dec 2007 14:06:30 +0100 (CET) Received: from trajan.apartia.fr (trajan.apartia.fr [10.0.3.121]) by zenon.apartia.fr (Postfix) with ESMTP id A92CD59C5B42A for ; Mon, 17 Dec 2007 14:06:29 +0100 (CET) Received: by trajan.apartia.fr (Postfix, from userid 1000) id 30B1A3CEED3B0; Mon, 17 Dec 2007 14:06:29 +0100 (CET) Date: Mon, 17 Dec 2007 14:06:29 +0100 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: passing a multiple join to a function? Message-ID: <20071217130629.GA8338@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.5.17 (2007-11-01) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/89 X-Sequence-Number: 29781 Hi, I've got this ugly case statement that I'd like to hide in a function: select ... case when d.start_date <= CURRENT_DATE and case when w.show_type in ('cinéma','livre') then d.start_date >= CURRENT_DATE - 21 else (d.end_date >= CURRENT_DATE or d.end_date is null) end then '0_actualite' when d.start_date > CURRENT_DATE then '1_agenda' else '2_archive' end as timing ... from story s join show w on (s.id_show = w.id_show) join show_date d on (d.id_show = w.id_show and d.start_date = (select d2.start_date from show_date d2 where d2.id_show = w.id_show order by d2.end_date >= CURRENT_DATE desc, d2.start_date limit 1) ) ... I could very well create a show_timing(int) function that accepts an id_show and performs its own, additional, multiple join complex query on story, show_date, and show. Is there a way of feeding enough data to the function to avoid another query? Thanks,