Solved

SQL UNION TABLE QUESTION

Posted on 2012-03-27
8
315 Views
Last Modified: 2012-03-29
I have mulptiple tables and would like to combine then

table1
Symbol shares
MSFT    100        
INTL      200      

table2
Symbol shares
MSFT    100    
INTL      200    
.
.

What would be a good way to produce a summary table that give me the following results?

Symbol  table1 table2  ...
MSFT      100      100
INTL       200      200

Thanks!
0
Comment
Question by:chrisli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37773242
Best do it with a join:

select t1.symbol, t1.col as Table1,  t2.col as Table2
from Table1 t1
left join table2 t2 on t1.symbol = t2.symbol
0
 

Author Comment

by:chrisli
ID: 37773328
In my case it is not so easy, since each table(table1, table2, table3) is produced by this sql query and it would be too complicated for me using your method to join multiple tables...

SELECT     Symbol, SUM(tradeQuantity) AS expr1
FROM         table1
WHERE     (accountNo, 9 IN
                          (SELECT     AccountNo
                            FROM         othertable
                            WHERE      (Type = 'cash'))
GROUP BY Symbol
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37773392
Insert each result into a temporary table and then do your joins from there.

SELECT     Symbol, SUM(tradeQuantity) AS expr1
into #t1
FROM         table1

WHERE     (accountNo, 9 IN
                          (SELECT     AccountNo
                            FROM         othertable
                            WHERE      (Type = 'cash'))
GROUP BY Symbol


select t1.Symbol, expr1 as Table1 from
#t1 t1
join #t2 t2 on t1.Symbol = t2.symbol
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:gpizzuto
ID: 37773394
You can use the PIVOT command in your generation.query
0
 
LVL 32

Assisted Solution

by:bhess1
bhess1 earned 100 total points
ID: 37774160
This would seem to be a good place to use a PIVOT select, such as this (you need to correct the labels, field names and perhaps some other details):

SELECT Symbol, [cash], [account], [deferred]...
FROM (
	SELECT Symbol, tradeQuantity, [type]
	FROM table1 t1
	INNER JOIN othertable AS ot
		ON ot.AccountNo = t1.AccountNo
	) AS P
PIVOT (
	SUM(tradeQuantity)
	FOR [type] IN ([cash], [account], [deferred],....)
	) AS pvt
ORDER BY Symbol

Open in new window

0
 
LVL 18

Expert Comment

by:deighton
ID: 37775381
Use union all to effectively create a single table, then use GROUP BY and SUM to amalgamate results into one row per symbol, with the total of the rows from both tables.

SELECT U.Symbol, SUM(U.shares) FROM (SELECT symbol, shares from table1 UNION ALL SELECT symbol, shares from table2) AS U GROUP BY U.symbol
0
 
LVL 18

Accepted Solution

by:
deighton earned 400 total points
ID: 37775424
sorry, didn't read the question properly

;WITH CTE1 AS
(
SELECT     Symbol, SUM(tradeQuantity) AS expr1
FROM         table1
WHERE     (accountNo, 9 IN
                          (SELECT     AccountNo
                            FROM         othertable
                            WHERE      (Type = 'cash'))
GROUP BY Symbol
),
 CTE2 AS
(
SELECT     Symbol, SUM(tradeQuantity) AS expr1
FROM         table2
WHERE     (accountNo, 9 IN
                          (SELECT     AccountNo
                            FROM         othertable
                            WHERE      (Type = 'cash'))
GROUP BY Symbol
),
CTE3 AS
(
SELECT     Symbol, SUM(tradeQuantity) AS expr1
FROM         table1
WHERE     (accountNo, 9 IN
                          (SELECT     AccountNo
                            FROM         othertable
                            WHERE      (Type = 'cash'))
GROUP BY Symbol
)
SELECT U.Symbol, SUM(U.t1) as tab1, SUM(U.t2) as tab2, SUM(U.t3) as tab3 FROM
          (SELECT symbol, shares as t1, 0 as t2, 0 as t3 from CTE1
          UNION ALL
          SELECT symbol, 0 as t1, shares as t2, 0 as t3 from CTE2
          UNION ALL
          SELECT symbol, 0 as t1, 0 as t2, shares as t3 from CTE3) AS U GROUP BY U.symbol
 

use CTE's to make all your 'tables' (as many as you want), then union each query into its appropriate column and summarise
0
 

Author Comment

by:chrisli
ID: 37782210
Thanks works great
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question