Solved

sql , sum UNION results of 1 column

Posted on 2013-01-18
6
512 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now