Solved

SQL UNION TABLE QUESTION

Posted on 2012-03-27
8
310 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

809 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