dbTalk Databases Forums  

Joining columns from multiple tables

comp.databases comp.databases


Discuss Joining columns from multiple tables in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Steve
 
Posts: n/a

Default Joining columns from multiple tables - 02-18-2004 , 07:43 PM






Hi,

I have two tables. Table1 has columns: jobid, time, cpu_perc. Table2 has
columns: cpu, time, load_ave.

The column 'time' on both tables hold the same values. However, Table1
holds other rows as well and so I need to select rows from both tables
that match 'time' that is in table2. The following query works:

SELECT time, cpu_perc FROM Table1 WHERE jobid = 53 AND time IN (SELECT
time FROM Table2 WHERE cpu = 400) ORDER BY time ASC;

How can I get 'load_ave' from the second table to be 'joined' with the
restul? Currently I only get the columns time and cpu_perc shown. I
tried doing a 'RIGHT OUTER JOIN' but it's giving me a cartesian result.
Can someone please help me out with the query?

Thanks!


Steve


Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Joining columns from multiple tables - 02-19-2004 , 03:08 AM






Steve <nospam@nopes> wrote

Quote:
Hi,

I have two tables. Table1 has columns: jobid, time, cpu_perc. Table2 has
columns: cpu, time, load_ave.

The column 'time' on both tables hold the same values. However, Table1
holds other rows as well and so I need to select rows from both tables
that match 'time' that is in table2. The following query works:

SELECT time, cpu_perc FROM Table1 WHERE jobid = 53 AND time IN (SELECT
time FROM Table2 WHERE cpu = 400) ORDER BY time ASC;

How can I get 'load_ave' from the second table to be 'joined' with the
restul? Currently I only get the columns time and cpu_perc shown. I
tried doing a 'RIGHT OUTER JOIN' but it's giving me a cartesian result.
Can someone please help me out with the query?

Thanks!


Steve
Untested, but you could try:

select t1.time, t1.cpu_perc, t2.load_avg from (select time, load_avg
from table2 where cpu = 400) as t2(time, load_avg) where t1.time =
t2.time order by t1.time asc

If this is a frequent query, concider defining a view (or if your db
supports it , a table function) for t2


HTH
/Lennart


Reply With Quote
  #3  
Old   
Kristian Damm Jensen
 
Posts: n/a

Default Re: Joining columns from multiple tables - 02-19-2004 , 03:47 AM



Lennart Jonsson wrote:
Quote:
Steve <nospam@nopes> wrote in message
news:<403414fc$1 (AT) clarion (DOT) carno.net.au>...
Hi,

I have two tables. Table1 has columns: jobid, time, cpu_perc. Table2
has columns: cpu, time, load_ave.

The column 'time' on both tables hold the same values. However,
Table1 holds other rows as well and so I need to select rows from
both tables that match 'time' that is in table2. The following query
works:

SELECT time, cpu_perc FROM Table1 WHERE jobid = 53 AND time IN
(SELECT time FROM Table2 WHERE cpu = 400) ORDER BY time ASC;

How can I get 'load_ave' from the second table to be 'joined' with
the restul? Currently I only get the columns time and cpu_perc
shown. I tried doing a 'RIGHT OUTER JOIN' but it's giving me a
cartesian result. Can someone please help me out with the query?

Thanks!


Steve

Untested, but you could try:

select t1.time, t1.cpu_perc, t2.load_avg from (select time, load_avg
from table2 where cpu = 400) as t2(time, load_avg) where t1.time =
t2.time order by t1.time asc

Reformated to make it readable:

select t1.time, t1.cpu_perc, t2.load_avg
from
(select time, load_avg
from table2 where cpu = 400) as t2(time, load_avg)
where t1.time = t2.time
order by t1.time asc

This is getting silly. Since you don't define t1 in the from clause, it
won't parse. And there's no reason to mess thing up with a subselect.


What the OP could try was:

SELECT t1.time, t1.cpu_perc, t2.load_avg
FROM Table1 t1
, Table2 t2
WHERE t1.jobid = 53
AND t2.cpu = 400
AND t1.time = t2.time
ORDER BY t1.time ASC;


--
Kristian Damm Jensen damm (at) ofir (dot) dk
Those who desire to give up Freedom in order to gain Security, will not
have, nor do they deserve, either one. -- Thomas Jefferson



Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.