Re: Select Union

From: Nick Barr <nicky(at)chuckie(dot)co(dot)uk>
To: Randall Skelton <skelton(at)brutus(dot)uwaterloo(dot)ca>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Select Union
Date: 2004-04-01 16:29:29
Message-ID: 406C4369.9010506@chuckie.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Randall Skelton wrote:

> I have a number of tables with the general structure:
>
> Column | Type | Modifiers
> -----------+--------------------------+-----------
> timestamp | timestamp with time zone |
> value | double precision |
> Indexes: tbl__timestamp
>
> and I would like to find the union of the timestamps. Something like:
>
> select timestamp from cal_quat_1 WHERE timestamp BETWEEN '2004-02-01
> 00:03:30' AND '2004-02-01 00:04:00' UNION select timestamp from
> cal_quat_2 WHERE timestamp BETWEEN '2004-02-01 00:03:30' AND
> '2004-02-01 00:04:00' UNION select timestamp from cal_quat_4 WHERE
> timestamp BETWEEN '2004-02-01 00:03:30' AND '2004-02-01 00:04:00'
> UNION select timestamp from cal_quat_4 WHERE timestamp BETWEEN
> '2004-02-01 00:03:30' AND '2004-02-01 00:04:00' UNION select timestamp
> from cal_ccd_temp WHERE timestamp BETWEEN '2004-02-01 00:03:30' AND
> '2004-02-01 00:04:00';
>
> Is there a less shorter, less redundant way of writing this?
>
> Cheers,
> Randall
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)

SELECT t1.timestamp FROM (
SELECT timestamp FROM cal_quat_1 UNION
SELECT timestamp FROM cal_quat_2 UNION
SELECT timestamp FROM cal_quat_3 UNION
SELECT timestamp FROM cal_quat_4 UNION
SELECT timestamp FROM cal_ccd_temp
) t1 WHERE
t1.timestamp BETWEEN '2004-02-01 00:03:30' AND '2004-02-01 00:04:00';

is technically shorter but I have no idea how well it will compare
performance wise with what you have got. If this runs a lot slower then
compare the output from explain analyze of the two queries.

Nick

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2004-04-01 16:35:04 Re: select distinct w/order by
Previous Message mike 2004-04-01 15:56:38 Problem restoring Database