If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Merge table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-28-04, 03:40
rageous rageous is offline
Registered User
 
Join Date: Apr 2004
Posts: 9
Merge table

I have multiple tables EveryWeek, EveryWeek1, EveryWeek2 etc which are all similar. I am trying to do a merge on these tables so I can query the merged table to get the sum, avg etc.
The way to do it I find on the web is this:
CREATE TABLE total (a INT NOT NULL, message CHAR(20), KEY(a)) TYPE=MERGE UNION=(t1,t2);

I have 2 problems with this (I dont know if these 2 problems can be avoided)
1. This creates a new table which I dont prefer (if there is another way).
2. This seems to force me to create the table, total, with all the columns in table t1 and t2. I need to create the table only with 2 out of 6 columns

I need my solution to work in both MySQL and Oracle

Thanks
Reply With Quote
  #2 (permalink)  
Old 04-28-04, 14:50
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
Why not create a view (even a materialized) with UNIONs to merge the tables (selecting the 2 columns from each table), then use the view in your select statement which you would use sum, avg, etc.
Reply With Quote
  #3 (permalink)  
Old 04-29-04, 00:58
rageous rageous is offline
Registered User
 
Join Date: Apr 2004
Posts: 9
I am using MySQL and I believe MySQL does not allow UNIONs
Reply With Quote
  #4 (permalink)  
Old 04-29-04, 06:09
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by rageous
I am using MySQL and I believe MySQL does not allow UNIONs
Is that really so? I thought it was supposed to be a serious contender in the RDBMS arena!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 04-29-04, 23:33
rageous rageous is offline
Registered User
 
Join Date: Apr 2004
Posts: 9
Correction. MySQl does support UNIONs. What is doesnt support is VIEWs
Reply With Quote
  #6 (permalink)  
Old 04-30-04, 05:00
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
No views in mySQL? Good grief...!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #7 (permalink)  
Old 04-30-04, 06:18
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
select sum(column), etc
from
(
inline view
)
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #8 (permalink)  
Old 04-30-04, 06:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by andrewst
Is that really so? I thought it was supposed to be a serious contender in the RDBMS arena!
now, now, tony, be nice

mysql actually is a serious contender

how many mysql installations should've gone to oracle but didn't?

whatever could the reason have been?



r123456, your inline view works only in mysql 4.1 (or should -- i can't test 4.1, seeing as how it's still "alpha" code and my host won't install it)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 04-30-04, 08:18
rageous rageous is offline
Registered User
 
Join Date: Apr 2004
Posts: 9
Let me rephrase my problem and explain my basic need.
I have multiple tables EveryWeek1, EveryWeek2, EveryWeek3 etc and all these tables have the same column structure. These tables contain records split over a period of time. When the user requests a top 10 between time A and time B, the records for this time interval could span across multiple tables (say EveryWeek1 and EveryWeek2). The number of records in these tables is roughly 500,000 per table.
Now I need to find the top 10 (or top 100) within time interval A and B. I hate to create a table/view of the entire tables that spans across A and B in order to get just 10 or 100 records.
Now is there a way to achieve this result without doing a merge or a join. This is on Java (though Id prefer to do this in at the DB level).
Reply With Quote
  #10 (permalink)  
Old 04-30-04, 09:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
"Now is there a way to achieve this result without doing a merge or a join"

no

needs a UNION

Code:
select foo, bar
  from (
select foo, bar from EveryWeek
union
select foo, bar from EveryWeek1
union
select foo, bar from EveryWeek2 
) as inlineview
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 04-30-04, 09:19
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
You can write a stored procedure (or does MySQL not support those either?) where you could dynamically prepare a SELECT statement concatenate the date range you want to pull data. The results would then have to be inserted into a temporary table (MySQL support those?). You could even make use of the system tables to loop through pulling the name of EveryWeek.. and concatenating them within your SELECT statement.

ie. 'SELECT a, b FROM ' || systemtable.tablename || FROM systemtable WHERE tablename like 'EveryWeek%'

Quite frankly, if you take MySQL out of the mix and go with Oracle, you would have had the information you need in a fraction of the time it has taken for suggestions to be posted/replied.
Reply With Quote
  #12 (permalink)  
Old 04-30-04, 09:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
no, mysql does not support stored procs

yes, mysql supports temp tables

i would love to go with oracle

zip it up and email it to me, would you please?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 04-30-04, 14:43
estefex estefex is offline
Registered User
 
Join Date: Jan 2004
Posts: 159
This is how you can tie in all your tables.

select column1, column2
into table1
from Everyweek
union
select column1, column2
from
Everyweek1
union
select column1, column2
from
Everyweek3

just specify the column that you want to choose.

I hope this hepls!
Reply With Quote
  #14 (permalink)  
Old 04-30-04, 14:54
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by r937
i would love to go with oracle

zip it up and email it to me, would you please?
Your mail server would choke!

-PatP
Reply With Quote
  #15 (permalink)  
Old 04-30-04, 16:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Pat Phelan
Your mail server would choke!
yes, i was making a point

you might also have said that, absent a license fee, it would be illegal
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On