Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id AE7B39FB55D for ; Thu, 12 Jul 2007 08:26:03 -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 79675-06 for ; Thu, 12 Jul 2007 08:25:53 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.249]) by postgresql.org (Postfix) with ESMTP id 746449FB490 for ; Thu, 12 Jul 2007 08:26:00 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c36so20964ana for ; Thu, 12 Jul 2007 04:25:56 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=eJoeJ3YamfEN5HYxvFsqHg3bxHxFFIX2d1MjCVQ6rbU3G0y41EYlWvqyuWjeiIUXeqQaCcRPTqwueJmmErQNXREFNMlkG8EJVAkbWD+oppxSCsX7eJeaXhelpa2Jem9G4ht3Eo7H5uZ0B7iu/g6InGegE6Ny3LUC3EOGGmDIEtM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=XY4/RTXfuH9Yv4IazFSfNvV6TqBZoxt8+1NCcslgvQR5lbuELI/oexifn3Cpmlpzk6uMcMJMGxooJCRwRFdAWRBQlotug9i9rRTdl+peHbZr/kRvcRjmEDctutYygfAKiUdEgb8vZs/gUOFYSDdBJ5TEpI5S80BgSqatSV6MGaY= Received: by 10.142.187.2 with SMTP id k2mr36264wff.1184239555561; Thu, 12 Jul 2007 04:25:55 -0700 (PDT) Received: by 10.142.88.4 with HTTP; Thu, 12 Jul 2007 04:25:55 -0700 (PDT) Message-ID: <162867790707120425p42e6de45k1342107418c80498@mail.gmail.com> Date: Thu, 12 Jul 2007 13:25:55 +0200 From: "Pavel Stehule" To: Ragnar Subject: Re: Using case or if to return multiple rows Cc: "Ashish Karalkar" , pgsql-sql@postgresql.org In-Reply-To: <1184233928.5778.117.camel@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <005401c7c450$3d6670e0$170211ac@LIONKING.COM> <1184233928.5778.117.camel@localhost.localdomain> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/63 X-Sequence-Number: 28798 > > > > > > 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' > maybe little bit more readable form: select * from table1 where exists (select 1 from table3 where date = 'xxxx') union all select * from table2 where exists (select 1 from table3 where date = 'xxxx'); it's possible with one big disadvantage. This query will do seq scan both tables and it can be slow on big tables. Regards Pavel Stehule