Solved

SQL UNION TABLE QUESTION

Posted on 2012-03-27
8
294 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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
 
LVL 8

Expert Comment

by:gpizzuto
Comment Utility
You can use the PIVOT command in your generation.query
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 32

Assisted Solution

by:bhess1
bhess1 earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks works great
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now