Re: Elegant SQL solution: - Mailing list pgsql-sql

From Ian Barwick
Subject Re: Elegant SQL solution:
Date
Msg-id 200306081354.17591.barwick@gmx.net
Whole thread Raw
In response to Elegant SQL solution:  (Chris Gamache <cgg007@yahoo.com>)
List 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@gmx.net




pgsql-sql by date:

Previous
From: Rod Taylor
Date:
Subject: Re: Domains and Joins
Next
From: Bruno Wolff III
Date:
Subject: Re: "Join" on delimeter aggregate query