Re: SQL query...

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Mario Weilguni <mweilguni(at)sime(dot)com>
Cc: jerome(at)gmanmi(dot)tv, pgsql-general(at)postgresql(dot)org
Subject: Re: SQL query...
Date: 2003-02-19 15:28:12
Message-ID: 20030219152812.GB17434@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Feb 19, 2003 at 14:38:53 +0100,
Mario Weilguni <mweilguni(at)sime(dot)com> wrote:
> >if i have 5 tables and all of it has column X and i wanted to get unique
> >values in column X for all tables..
>
> >can i do it in one query?
>
> sure. this is one way to do this:
> select distinct foo.X from (
> select X from table1
> union
> select X from table2
> ....
> ) as foo;
>
> if the values are unique within every table, then you might want to omit the
> surrounding select.

Union's will already only return unique values. So you don't need the outer
select distinct. You might get better performance by using union alls
with select distinct, since only one sort will be needed. For the case
where you know they are all unique, you can use union alls without the
select distinct.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Mark Cave-Ayland 2003-02-19 15:35:18 Re: 7.3.1 takes long time to vacuum table?
Previous Message Tom Lane 2003-02-19 15:12:51 Re: postgres error reporting