Solved

SQL UNION TABLE QUESTION

Posted on 2012-03-27
8
318 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:Brendt Hess
Brendt Hess 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

631 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