pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ian Barwick <barwick@gmx.net>
To: cgg007@yahoo.com
To: pgsql-sql@postgresql.org
Subject: Re: Elegant SQL solution:
Date: Sun, 8 Jun 2003 13:54:17 +0200
Message-ID: <200306081354.17591.barwick@gmx.net> (raw)
In-Reply-To: <20030606162635.97132.qmail@web13807.mail.yahoo.com>
References: <20030606162635.97132.qmail@web13807.mail.yahoo.com>

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





view thread (4+ messages)

Message-ID: <200306081354.17591.barwick@gmx.net>
Permalink:  ../200306081354.17591.barwick@gmx.net/
Also on:    postgresql.org/message-id/200306081354.17591.barwick@gmx.net

 · 

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: barwick@gmx.net, cgg007@yahoo.com
  Subject: Re: Elegant SQL solution:
  In-Reply-To: <200306081354.17591.barwick@gmx.net>

* 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