?
Solved

sql , sum UNION results of 1 column

Posted on 2013-01-18
6
Medium Priority
?
522 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 2000 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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 article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

765 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