Re: Elegant SQL solution:

From: Ian Barwick <barwick(at)gmx(dot)net>
To: cgg007(at)yahoo(dot)com, pgsql-sql(at)postgresql(dot)org
Subject: Re: Elegant SQL solution:
Date: 2003-06-08 11:54:17
Message-ID: 200306081354.17591.barwick@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)gmx(dot)net

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Rod Taylor 2003-06-08 12:23:39 Re: Domains and Joins
Previous Message Bruno Wolff III 2003-06-08 11:37:02 Re: "Join" on delimeter aggregate query