Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id F121E19713C6 for ; Tue, 6 Dec 2011 13:32:24 -0400 (AST) Received: from iota.marktest.pt ([80.251.174.118] helo=hermes.marktest.pt) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RXysR-00014f-An for pgsql-sql@postgresql.org; Tue, 06 Dec 2011 17:32:24 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by hermes.marktest.pt (Postfix) with ESMTP id 38AE51A8154; Tue, 6 Dec 2011 17:32:09 +0000 (WET) X-Virus-Scanned: amavisd-new at marktest.pt Received: from hermes.marktest.pt ([127.0.0.1]) by localhost (hermes.marktest.pt [127.0.0.1]) (amavisd-new, port 11124) with ESMTP id ej1Ieta0gFk8; Tue, 6 Dec 2011 17:32:09 +0000 (WET) Received: from Proteu (proteu.marktest.pt [10.61.90.54]) by hermes.marktest.pt (Postfix) with ESMTP id 04ECF1A8153; Tue, 6 Dec 2011 17:32:08 +0000 (WET) Message-ID: <98AAAADF76F646818830175C98637375@marktestcr.marktest.pt> From: "Oliveiros d'Azevedo Cristina" To: "Scott Swank" Cc: "Abhinandan Raghavan" , References: <4EDE1F3F.6060807@unige.ch><37CE275E52C24BA189FDD6BA0B905F95@marktestcr.marktest.pt> Subject: Re: Self-Join Date: Tue, 6 Dec 2011 17:32:07 -0000 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5512 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201112/7 X-Sequence-Number: 36068 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 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 : > > 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 JOIN original b > ON a.name = b.name > AND b.attribute = 'Weight' > GROUP BY a.name > ) weight > NATURAL JOIN > ( > SELECT a.name,MAX(b.value) as age > FROM original a > LEFT JOIN > original > b > ON a.name = b.name > AND b.attribute = 'Age' > GROUP BY a.name > ) age > > The thing is that it doesn't scale well if you have many more items beyond > three... > > Best, > Oliveiros > > ----- Original Message ----- > From: Abhinandan Raghavan > To: pgsql-sql@postgresql.org > Sent: Tuesday, December 06, 2011 1:57 PM > Subject: [SQL] Self-Join > > 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 normally created a view > in > the following way: > > > SELECT A.NAME, > A.VALUE AS WEIGHT, > B.VALUE AS HEIGHT, > C.VALUE AS AGE > > FROM NAV A, > NAV B, > NAV C > > WHERE A.NAME = B.NAME > AND A.NAME = C.NAME > AND A.ATTRIBUTE = 'Weight' > AND B.ATTRIBUTE = 'Height' > AND C.ATTRIBUTE = 'Age' > > > The only problem when I create a view with the above select statement is > that when there are no entries for the field name "AGE" (in the case of > David), then the row does not get displayed. What's the way out in > Postgresql? I know the way it is addressed in Oracle but it doesn't seem > to > work in Postgresql. > > Thanks. > > Abhi > > > ________________________________ > > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql