Re: a wierd query

From: Peter Childs <blue(dot)dragon(at)blueyonder(dot)co(dot)uk>
To: "ashok(at)kalculate(dot)com" <ashok(at)kalculate(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: a wierd query
Date: 2004-05-13 09:17:46
Message-ID: Pine.LNX.4.58.0405131014500.15412@bluedragon.homelinux.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 13 May 2004, ashok(at)kalculate(dot)com wrote:

> hi
>
> i have a wierd problem and i require an equally weird query.
> 1) backgound
> Table test:
> CREATE TABLE main_table (
> string_A varchar( 20),
> string_B varchar( 20),
> );
> -- both columns are identical in nature and usage
> INSERT INTO main_table VALUES('abcd','qrst');
> INSERT INTO main_table VALUES('efgh','efgh');
> INSERT INTO main_table VALUES('ijkl','abcd');
> INSERT INTO main_table VALUES('abcd','ijkl');
> INSERT INTO main_table VALUES('qrst','uvwx');
>
> 2) problem:
>
> i require a query that gives me a result set of the form
>
> 'abcd'
> 'efgh'
> 'ijkl'
> 'qrst'
> 'uvwx'
>
> that is i require the dictinct values from (visualizing each column
> result as a set) the union of the two columns
>
> 3) questions
>
> a) is a query like this possible that can give me the desired result
> b) if so what would it be.
>
> 4) remarks
>
> i can get the solution using a temporary table and with repeated
> "insert into temporary select $column from main_table"
>
>

select distinct t from (select string_A as t from main_table union
select string_B as t from main_table);

or

select f from (select A as t from main_table union select B as t from
main_table) group by t order by t;

hope that helps

Peter Childs

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Yasir Malik 2004-05-13 13:24:10 Re: a wierd query
Previous Message Silke Trißl 2004-05-13 08:41:10 Re: a wierd query