X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (developer.postgresql.org [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id CACB0EFF61 for ; Sun, 8 Jun 2003 11:55:22 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 31371-04 for ; Sun, 8 Jun 2003 11:55:12 +0000 (GMT) Received: from post.webmailer.de (natsmtp01.webmailer.de [192.67.198.81]) by svr1.postgresql.org (Postfix) with ESMTP id F007EEFF45 for ; Sun, 8 Jun 2003 11:55:11 +0000 (GMT) Received: from iwate.ianb.homelinux.net (pD9517DFC.dip.t-dialin.net [217.81.125.252]) by post.webmailer.de (8.12.8/8.8.7) with ESMTP id h58BtDQJ003345; Sun, 8 Jun 2003 13:55:14 +0200 (MEST) From: Ian Barwick To: cgg007@yahoo.com, pgsql-sql@postgresql.org Subject: Re: Elegant SQL solution: Date: Sun, 8 Jun 2003 13:54:17 +0200 User-Agent: KMail/1.5.1 References: <20030606162635.97132.qmail@web13807.mail.yahoo.com> In-Reply-To: <20030606162635.97132.qmail@web13807.mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200306081354.17591.barwick@gmx.net> X-Archive-Number: 200306/59 X-Sequence-Number: 13817 On Friday 06 June 2003 18:26, Chris Gamache wrote: > I could create a one-column table with values 1 - 12 in it, and select from > that table with a where clause matching "month". I could also create a view > "SELECT 1 UNION SELECT 2 UNION ..." and select against the view. There MUST > be a more elegant way to do this. You probably need a pivot table (the one-column table with values 1 - 12). Oracle Magazine had a useful article on this subject (relevant for none-Oracle SQL too) a while back: http://otn.oracle.com/oramag/oracle/02-sep/o52sql.html (registration probably required). Of course you could also use a set returning function a la: CREATE OR REPLACE FUNCTION months() RETURNS SETOF INT AS ' BEGIN FOR i IN 1..12 LOOP RETURN NEXT i; END LOOP; RETURN; END;' LANGUAGE 'plpgsql'; Ian Barwick barwick@gmx.net