Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 12th, 2006, 02:53 PM
danyeungw danyeungw is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 5 danyeungw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 9 sec
Reputation Power: 0
Sum for one to many in SQL.

Here is the SQL statement:

select Distributor.Name, sum(AllocationFund.Amount) as Allocaiton,
round(sum(AllocationFund.Amount), 0) as RequestAmount,
round(sum(AllocationFund.Amount) - sum(Request.Amount), 0) as Balance
from Distributor inner join AllocationFund
on Distributor.DistributorID = AllocationFund.DistributorID
left outer join Request
on AllocationFund.AllocTypeID = Request.AllocTypeID
and Distributor.DistributorID = Request.distributorID
group by Distributor.Name
order by Distributor.Name

Data for Distributor1 (Result)
Allocation Fund 1 = 45000
Allocation Fund 2 = 3000
Allocation Fund 3 = 1875

Request Amount for Fund 1 = 684.76
Request Amount for Fund 1 = 29600
Request Amount for Fund 2 = 1984.85
Request Amount for Fund 2 = 11998.52
Request Amount for Fund 2 = 444.58
Request Amount for Fund 2 = 375.14

The Result should be: Name = Distributor1
Allocation = 49875 (45000 + 3000 + 1875)
RequestAmount = 45088 (684.76 + 29600 + 1984.85 + 11998.52 + 444.58 + 375.14)
Balance = 4787 (49875 - 45088)

But I got the following result:
Result: Name = Distributor1
Allocation = 103875 (45000 * 2 + 3000 * 4 + 1875)
RequestAmount = 45090.0000
Balance = 58790.0000

How can I get the correct result?

Thanks.
DanYeung

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Sum for one to many, change NULL to zero in SQL.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT