Re: join and dynamic view - Mailing list pgsql-sql

From Christoph Haller
Subject Re: join and dynamic view
Date
Msg-id 3DFF1328.13B365D6@rodos.fzk.de
Whole thread Raw
In response to join and dynamic view  (Gary Stainburn <gary.stainburn@ringways.co.uk>)
Responses Re: join and dynamic view  (Gary Stainburn <gary.stainburn@ringways.co.uk>)
Re: join and dynamic view  (Tomasz Myrta <jasiek@klaster.net>)
List pgsql-sql
>
> is it possible to make a dynamically declare a view based on a table?
>
Yes, by all means.

>
> Is it possible to now define a view such that it returns:
>
> select * from myview;
> sid  | Name    | OPS | MPD
> -----+---------+-----+-----
>  1   | Rod     |     |  3
>  2   | Jayne   |  2  |  5
>  3   | Freddie |  3  |
>
> and if I add another row to depts, that the new row would be included?

>
^^^^^^^^^^^^^^^^ you mean column, don't you?
The closest query I can get so far is
SELECT staff.*,      CASE dsdesc WHEN 'OPS' THEN rrank ELSE NULL END AS "OPS",      CASE dsdesc WHEN 'MPD' THEN rrank
ELSENULL END AS "MPD"
 
FROM staff,depts,ranks WHERE sid=rsid AND did=rdid ;
sid |  sname  | OPS | MPD
-----+---------+-----+-----  1 | Rod     |     |   3  2 | Jayne   |     |   2  2 | Jayne   |   5 |  3 | Freddie |   3
|
(4 rows)

but
sid |  sname  | OPS | MPD
-----+---------+-----+-----  1 | Rod     |     |   3  2 | Jayne   |   5|   2  3 | Freddie |   3 |
(3 rows)

is what you want (I suppose Jayne's 2 in OPS and 5 in MPD is a mismatch
of yours).
As soon as you are somebody else can tell me how to merge Jayne's two
rows into one,
I'm sure I can write a plpgsql function to dynamically create the view
you're looking for.

Regards, Christoph



pgsql-sql by date:

Previous
From: "Tarun Galarani"
Date:
Subject: Difference between DB2 7.0 & latest version of PostgresSQL?
Next
From: Gary Stainburn
Date:
Subject: Re: join and dynamic view