473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select Union

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 YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #1
3 6768
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 YourEmailAddres sHere" to ma*******@postg resql.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

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 23 '05 #2
As you suggested, while this is much shorter in length, it is
considerably longer in execution. Despite each timestamp being
indexed, each table has over 4M rows which makes this too slow to be
practical. An 'explain analyze' of this would be nice but the shear
length of time it is taking suggests it is looping through each row for
the union and then restricting to the 10 rows of interest.

Cheers,
Randall

On 1 Apr 2004, at 11:29, Nick Barr wrote:
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

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #3
Randall Skelton <sk*****@brutus .uwaterloo.ca> writes:
As you suggested, while this is much shorter in length, it is
considerably longer in execution.

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';


Do you actually need UNION, or would UNION ALL do? (The latter doesn't
try to eliminate duplicates.)

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7926
by: Chris Becker | last post by:
I have the following query: SELECT Month, Sum(Hits) AS Hits FROM tblHits GROUP BY Month ORDER BY Month Unfortunately it only returns rows for months that have data assigned to them. How can I tweak this so that months 1-12 are returned, and Hits = 0 for months with no data in the base table?
4
6955
by: Elroyskimms | last post by:
Using SQL 2000... tblCustomer: CustomerID int CompanyName varchar(20) HasRetailStores bit HasWholesaleStores bit HasOtherStores bit tblInvoiceMessages:
17
5033
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by cust_no, ded_type_cd, chk_no)
1
3347
by: jtwright | last post by:
I've got a view that creates a parent child relationship, this view is used in Analysis Services to create a dimension in a datastore. This query tends to deadlock after about 10 days of running smoothly. Only way to fix it is to reboot the box, I can recycle the services for a quick fix but that usually only works for the next 1-2 times I call the view. This view is used to create a breakdown of the bill-to locations from...
3
5732
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int pointing to the sentence they come from (a column called "regret"). I also have a table of stop words (called "GenericStopWords") that contains the words I do not want to consider. That table has a single column called "word". I started off using a...
3
6738
by: Ker | last post by:
I have a query that works great. It gives me the min for multiple fields. Within this query, I also need to get the max of some fields too. I currently have output of Date Name Min Start 9/1/03 Walker Rhines 7:00AM I also need Max such as Max End 3:00PM
6
3396
by: Apaxe | last post by:
In the database i have a table with this information: key_id =1 key_desc =43+34+22+12 I want sum the values in key_desc. Something like: SELECT key_desc FROM table But the result of the select was "111" and not "43+34+22+12".
2
2821
by: rickcf | last post by:
I have two tables. Table A is the main table I need to update and table B is the update table with the new data. Table B contains only two fields- fielda (also contained in table A for the linking but also is the field that needs changed) and a second field (fieldb) which is the NEW value of fielda that I need to change in TableA. Thus I need a combination UPDATE/SELECT statement to link the two tables togeather to update table A with the new...
4
2630
by: Manikrag | last post by:
Hi Team, Is it possible to sort select query based on input string? I am looking for somthing like: select TOP 20 PREFERRED_NAME from FRS_TABLE where Lower(PREFERRED_NAME) like Lower('%shar%') order by PREFERRED_NAME LIKE '%shar%'
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.