Solved

SQL UNION TABLE QUESTION

Posted on 2012-03-27
8
309 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Optimization 14 46
Return 0 on SQL count 24 30
How to create a unique rule restriction on a table for two fields 16 84
Proper Case SQL Command 2 13
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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 the fundamental information of how to create a table.

831 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