Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

join multiple tables

Status
Not open for further replies.

xdia

MIS
May 23, 2002
20
0
0
US
Hello I need ways to accomplish the following

tbl1
name sales
a 10
b 20
c 30

tbl2
name sales
b 50
c 60
d 70


tbl3
name sales

a 30
c 40
x 40

I want it to be like below:

tbl1 tbl2 tbl3
a 10 30
b 20 50
c 30 60 40
d 70
x 40



Thanks in advance~~
 
One more thing all data comes from one table and tble a,b,c I have created from different period such as this year, last year, this month. What would be the best way to do this???
 
use crosstab on this query --

Code:
select name, sales as tbl1
  from tbl1
union all
select name, sales as tbl2
  from tbl2
union all
select name, sales as tbl3
  from tbl3

in fact, you could probably adapt this so that you wouldn't need to split the data out of the original table into three tables, you could just do it in three WHERE clauses above...

rudy
 
whoops, that was sloppy

try this instead --

Code:
select name, sales, 'tbl1' as tablename
  from tbl1
union all
select name, sales, 'tbl2' as tablename
  from tbl2
union all
select name, sales, 'tbl3' as tablename
  from tbl3
 
Hey thanks for the reply and try the second method but it gives me the sum of the sales amout instead of the result I want. Am I doing something wrong?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top