Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 796E89FB51A for ; Thu, 12 Jul 2007 06:49:44 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 26797-03 for ; Thu, 12 Jul 2007 06:49:34 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mx1.ipf.is (mack.ipf.is [85.197.192.164]) by postgresql.org (Postfix) with ESMTP id 5DFE49FB7ED for ; Thu, 12 Jul 2007 06:49:40 -0300 (ADT) Received: (qmail 29762 invoked by uid 1010); 12 Jul 2007 09:49:34 +0000 Received: from 88.149.106.19 by mack (envelope-from , uid 1008) with qmail-scanner-1.25-st-qms (clamdscan: 0.88.4/1639. spamassassin: 3.1.4. perlscan: 1.25-st-qms. Clear:RC:1(88.149.106.19):SA:0(-2.3/4.5):. Processed in 0.318842 secs); 12 Jul 2007 09:49:34 -0000 X-Antivirus-HIVE-Mail-From: gnari@hive.is via mack X-Antivirus-HIVE: 1.25-st-qms (Clear:RC:1(88.149.106.19):SA:0(-2.3/4.5):. Processed in 0.318842 secs Process 29755) Received: from dsl-149-106-19.hive.is (HELO ?192.168.1.33?) (88.149.106.19) by mx1.ipf.is with SMTP; 12 Jul 2007 09:49:34 +0000 Subject: Re: Using case or if to return multiple rows From: Ragnar To: Ashish Karalkar Cc: pgsql-sql@postgresql.org In-Reply-To: <005401c7c450$3d6670e0$170211ac@LIONKING.COM> References: <005401c7c450$3d6670e0$170211ac@LIONKING.COM> Content-Type: text/plain Date: Thu, 12 Jul 2007 09:52:08 +0000 Message-Id: <1184233928.5778.117.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.8.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/62 X-Sequence-Number: 28797 On fim, 2007-07-12 at 12:15 +0530, Ashish Karalkar wrote: > I want to select data from two diffrent table based on third tables > column > somthing like: > > > select case when t3.date='xxxx' then > select * from table1 > else > select * from table 2 > from table3 t3 where t3.date='xxxxx' > > Problem is that I have to do it in Plain SQL. you problem is not quite clear. do you want to output all rows from table1 (or table2) for each row of table3? or does table2 only have one row with date='xxxxx' ? is 'xxxx' the same date as 'xxxxx' in your example? if so, how can table2's columns be selected. are you looking for something like: select * from table1 where (select date from table3)='xxxx' UNION ALL select * from table2 where (select date from table3)<>'xxxx' this might work if table1 and table2 have compatible schemas, and table3 only has 1 row, and table3.date is NOT NULL if this is not what you are looking for, you need to be more clear. gnari