Re: [SQL] Self-Join

2011-12-06 Thread Bèrto ëd Sèra
Hi Abhinandan, I suppose you mean this: CREATE TABLE nav ( name varchar NOT NULL, attribute text NOT NULL, value numeric ); ALTER TABLE ONLY nav ADD CONSTRAINT nav_pkey PRIMARY KEY (name, attribute); insert into nav values ('James','Weight',70); insert into nav values ('James','Height',165); ins

Re: [SQL] Self-Join

2011-12-06 Thread Scott Swank
There are two problems with the OTLT approach (as well as EAV). One is laid out nicely by Tony. The second issue is that this big, generic table hides crucial information from the optimizer. If you cluster/order the data by the lookup type you can at least minimize page/block reads and improve dat

Re: [SQL] Self-Join

2011-12-06 Thread Oliveiros d'Azevedo Cristina
I have not. I've already skimmed through it. Indeed, it is very interesting Thanx , Scott Best, Oliver - Original Message - From: "Scott Swank" To: "Oliveiros d'Azevedo Cristina" Cc: "Abhinandan Raghavan" ; Sent: Tuesday, December 06, 2011 5:17 PM Subject: Re: [SQL] Self-Join

Re: [SQL] Self-Join

2011-12-06 Thread Scott Swank
Have you read Tony Andrew's 2004 piece on this approach? It is a classic. http://tonyandrews.blogspot.com/2004/10/otlt-and-eav-two-big-design-mistakes.html Scott On Tue, Dec 6, 2011 at 6:39 AM, Oliveiros d'Azevedo Cristina wrote: > Howdy, Abhinandan, > > A quick and dirty solution might be this

Re: [SQL] Self-Join

2011-12-06 Thread Oliveiros d'Azevedo Cristina
Howdy, Abhinandan, A quick and dirty solution might be this : SELECT * FROM ( SELECT a.name,MAX(b.value) as height FROM original a LEFT JOIN original b ON a.name = b.name AND b.attribute = 'Height' GROUP BY a.name ) height NATURAL JOIN ( SELECT a.name,MAX(b.value) as weigth FROM original a LEFT

Re: [SQL] Self-Join

2011-12-06 Thread Bèrto ëd Sèra
Hi Abhinandan, it's just the same outer join you'd do in Oracle, see: http://www.postgresql.org/docs/9.1/static/tutorial-join.html Bèrto On 6 December 2011 16:57, Abhinandan Raghavan wrote: > Hi, > > I'm looking to frame an SQL statement in Postgres for what's explained in > the attached imag

[SQL] Self-Join

2011-12-06 Thread Abhinandan Raghavan
Hi, I'm looking to frame an SQL statement in Postgres for what's explained in the attached image. The original table is at the top and is called NAV (Short for Name, Attribute, Value). I want to create a view (NWHA_View) involving values from within (presumably from a self join). I would've