Solved

sql , sum UNION results of 1 column

Posted on 2013-01-18
6
517 Views
Last Modified: 2013-01-18
hi everyone,

I have the following UNION statement with 3 columns.

Here is an example of results

111-12345      CDH3 CURNCY      -8.00
111-12345      CDH3 CURNCY      8.00

How do I rearrange my query, so the result is
111-12345                CDH3 CURNCY        0.00

Basically, just SUM the 3rd columns results.

Thanks in advance!!

SELECT dbo.TopdayTradesDB.Account_Number, dbo.TopdayTradesDB.BloombergSymbol, SUM(dbo.TopdayTradesDB.Qty_Net) As TopdayNet
FROM dbo.TopdayTradesDB
GROUP BY [Account_Number], BloombergSymbol
UNION
SELECT dbo.DailyPositions_JEFF.Account_Number, dbo.DailyPositions_JEFF.BloombergSymbol, SUM(dbo.DailyPositions_JEFF.Qty_Net) As TopdayNet
FROM dbo.DailyPositions_JEFF
GROUP BY [Account_Number], BloombergSymbol

Open in new window

0
Comment
Question by:solarissf
  • 3
  • 3
6 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 38794286
Throw it all in a subquery, and do a sum

SELECT a.AccountNumber, a.BloombergSymbol, SUM(a.TopdayNet) as TopDayNet_Sum
FROM (
	SELECT dbo.TopdayTradesDB.Account_Number, dbo.TopdayTradesDB.BloombergSymbol, SUM(dbo.TopdayTradesDB.Qty_Net) As TopdayNet
	FROM dbo.TopdayTradesDB
	GROUP BY [Account_Number], BloombergSymbol
	UNION
	SELECT dbo.DailyPositions_JEFF.Account_Number, dbo.DailyPositions_JEFF.BloombergSymbol, SUM(dbo.DailyPositions_JEFF.Qty_Net) As TopdayNet
	FROM dbo.DailyPositions_JEFF
	GROUP BY [Account_Number], BloombergSymbol) a
GROUP BY a.AccountNumber, a.BloombergSymbol

Open in new window

Also, UNION ALL may be faster than UNION, if your intent is not to weed out duplicates on both sides of the UNION
0
 

Author Comment

by:solarissf
ID: 38794302
my goal is to sum the net_qty in both table, by account and bloomberg symbol.
I only want 1 lot for each account and symbol..

do I still want UNIONALL?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38794311
The above groups by AccountNumber and BloombergSymbol, and sums the TopdayNet values.  

If that's not the behavior you need, then you'll have to spell it out in greater detail.

>my goal is to sum the net_qty in both table
Sum them separately, then UNION?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:solarissf
ID: 38794368
worked... thank you!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38794444
Thanks for the grade.  Good luck with your project.  -Jim

>do I still want UNIONALL?
UNION requires some extra overhead as it has to marry up the two sets and eliminate duplicates.

UNION ALL is used either when you need every row, (your case), or the two sets are distinct enough that you don't have to worry about it (SELECT name FROM burger_joints UNION ALL SELECT name FROM whorehouses).  UNION ALL does not have the overhead UNION does, so it runs faster.
0
 

Author Comment

by:solarissf
ID: 38794454
awesome./... and nice houses reference
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 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