Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql , sum UNION results of 1 column

Posted on 2013-01-18
6
Medium Priority
?
523 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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