Solved

sql , sum UNION results of 1 column

Posted on 2013-01-18
6
520 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
[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
  • 3
  • 3
6 Comments
 
LVL 66

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 66

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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