Thread: Using case or if to return multiple rows

Using case or if to return multiple rows

From
"Ashish Karalkar"
Date:
Hello all,
 
 
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.
 
Is there a anyway.
 
Thanks in Advance
 
With egards
Ashish....
 

Re: Using case or if to return multiple rows

From
"Pavel Stehule"
Date:
Hello

what is relation between t1 and t3 and t2 and t3? Which row from t3
specifies  value?  You cannot do it in plain SQL. SQL is well for set
operations. You can use plpgsql and SRF function.


-- table1 and table2 have to have same structure
CREATE OR REPLACE FUNCTION output_tab(date)
RETURNS SETOF table1 AS $$ DECLARE t1 table1; t2 table2;
BEGIN IF $1 = 'xxxx' THEN   FOR t1 IN SELECT * FROM table1 LOOP      RETURN NEXT t1;   END LOOP; ELSE   FOR t2 IN
SELECT* FROM table1 LOOP      RETURN NEXT t2;   END LOOP; END IF;RETURN;
 
END;
$$ LANGUAGE plpgsql;

and then
SELECT * FROM output_tab('xxxxx');

Regards
Pavel Stehule


2007/7/12, Ashish Karalkar <ashish.karalkar@info-spectrum.com>:
>
>
> Hello all,
>
>
> 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.
>
> Is there a anyway.
>
> Thanks in Advance
>
> With egards
> Ashish....
>


Re: Using case or if to return multiple rows

From
Ragnar
Date:
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







Re: Using case or if to return multiple rows

From
"Pavel Stehule"
Date:
> >
> >
> > 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


Re: Using case or if to return multiple rows

From
Tom Lane
Date:
"Pavel Stehule" <pavel.stehule@gmail.com> writes:
> it's possible with one big disadvantage. This query will do seq scan
> both tables and it can be slow on big tables.

No, it should be reasonably OK, because if the added condition doesn't
involve the tables being scanned it'll be turned into a one-time filter.
As an example using the regression database:

regression=# explain select * from tenk1 where 1 in (select f1 from int4_tbl)
union all
select * from tenk1 where 1 not in (select f1 from int4_tbl);                              QUERY PLAN
-------------------------------------------------------------------------Append  (cost=1.06..1118.13 rows=20000
width=244) ->  Result  (cost=1.06..459.06 rows=10000 width=244)        One-Time Filter: (hashed subplan)        ->  Seq
Scanon tenk1  (cost=0.00..458.00 rows=10000 width=244)        SubPlan          ->  Seq Scan on int4_tbl
(cost=0.00..1.05rows=5 width=4)  ->  Result  (cost=1.06..459.06 rows=10000 width=244)        One-Time Filter: (NOT
(hashedsubplan))        ->  Seq Scan on tenk1  (cost=0.00..458.00 rows=10000 width=244)        SubPlan          ->  Seq
Scanon int4_tbl  (cost=0.00..1.05 rows=5 width=4)
 
(11 rows)

The probe into int4_tbl will happen twice (but not more than that)
and whichever tenk1 scan gets discarded won't happen at all.  So
unless the test condition itself is horribly expensive this should
work as well as you could expect.
        regards, tom lane