Author |
Topic |
danyeung
Posting Yak Master
102 Posts |
Posted - 2006-09-19 : 11:04:57
|
The following SQL statement returned incorrect Allocation Amount. Please see the examples. Can you please revise the SQL statement?select Distributor.Name, sum(AllocationFund.Amount) as Allocaiton, sum(isNULL(Request.mnyAmount, 0)) RequestAmount,from Distributor inner join AllocationFundon Distributor.DistributorID = AllocationFund.DistributorIDleft outer join Requeston AllocationFund.AllocTypeID = Request.AllocTypeIDand Distributor.DistributorID = Request.distributorIDgroup by Distributor.Nameorder by Distributor.NameData for Distributor1Allocation Fund 1 = 45000Allocation Fund 2 = 3000Allocation Fund 3 = 1875Data in RequestRequest Amount for Fund 1 = 684.76Request Amount for Fund 1 = 29600Request Amount for Fund 2 = 1984.85Request Amount for Fund 2 = 11998.52Request Amount for Fund 2 = 444.58Request Amount for Fund 2 = 375.14The Result should be: Name = Distributor1Allocation = 49875 (45000 + 3000 + 1875)RequestAmount = 45088 (684.76 + 29600 + 1984.85 + 11998.52 + 444.58 + 375.14)But I got the following result:Result: Name = Distributor1Allocation = 103875 (45000 * 2 + 3000 * 4 + 1875)RequestAmount = 45088Thanks.DanYeung |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-19 : 11:13:12
|
try thisselect Distributor.Name, sum(AllocationFund.Amount) as Allocaiton, sum(isNULL(sum_mnyAmount, 0)) as RequestAmount,from Distributor inner join AllocationFund on Distributor.DistributorID = AllocationFund.DistributorID left outer join ( select distributorID, AllocTypeID, sum(mnyAmount) as sum_mnyAmount from Request group by distributorID, AllocTypeID ) req on AllocationFund.AllocTypeID = req.AllocTypeID and Distributor.DistributorID = req.distributorIDgroup by Distributor.Nameorder by Distributor.Name KH |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-19 : 11:15:58
|
Try to drop the alloctype on the join for Request.Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-19 : 11:18:56
|
[code]SELECT d.Name, af.AllocTypeID, ISNULL(af.Allocation, 0) Allocation, ISNULL(r.RequestAmount, 0) RequestAmountFROM Distributor dLEFT JOIN ( SELECT DistributorID, AllocTypeID, SUM(Amount) Allocation FROM AllocationFund GROUP BY DistributorID, AllocTypeID ) af ON af.DistributorID = d.DistributorIDLEFT JOIN ( SELECT DistributorID, AllocTypeID, SUM(mnyAmount) RequestAmount FROM Request GROUP BY DistributorID, AllocTypeID ) r ON r.DistributorID = af.DistributorID AND r.AllocTypeID = af.AllocTypeID[/code]Peter LarssonHelsingborg, Sweden |
 |
|
danyeung
Posting Yak Master
102 Posts |
Posted - 2006-09-19 : 11:49:38
|
quote: Originally posted by khtan try thisselect Distributor.Name, sum(AllocationFund.Amount) as Allocaiton, sum(isNULL(sum_mnyAmount, 0)) as RequestAmount,from Distributor inner join AllocationFund on Distributor.DistributorID = AllocationFund.DistributorID left outer join ( select distributorID, AllocTypeID, sum(mnyAmount) as sum_mnyAmount from Request group by distributorID, AllocTypeID ) req on AllocationFund.AllocTypeID = req.AllocTypeID and Distributor.DistributorID = req.distributorIDgroup by Distributor.Nameorder by Distributor.Name KH
Thanks.DanYeung |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-19 : 11:51:05
|
Peter LarssonHelsingborg, Sweden |
 |
|
ramoneguru
Yak Posting Veteran
69 Posts |
Posted - 2006-09-19 : 20:21:11
|
quote: Originally posted by Peso
Peter LarssonHelsingborg, Sweden
Yeah, I'm not hip on the lingo, could you please translate the above quote? Thanks.--Nick |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-20 : 00:58:30
|
It was a message to khtan, why beat me with a few minutes with the same solution.Peter LarssonHelsingborg, Sweden |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-20 : 01:03:05
|
It basically means shoot the yak  KH |
 |
|
danyeung
Posting Yak Master
102 Posts |
Posted - 2006-11-01 : 14:23:23
|
quote: Originally posted by khtan try thisselect Distributor.Name, sum(AllocationFund.Amount) as Allocaiton, sum(isNULL(sum_mnyAmount, 0)) as RequestAmount,from Distributor inner join AllocationFund on Distributor.DistributorID = AllocationFund.DistributorID left outer join ( select distributorID, AllocTypeID, sum(mnyAmount) as sum_mnyAmount from Request group by distributorID, AllocTypeID ) req on AllocationFund.AllocTypeID = req.AllocTypeID and Distributor.DistributorID = req.distributorIDgroup by Distributor.Nameorder by Distributor.Name KH
The above query returns all records in AllocationFund table althought there is no spending against the allocation fund in Request table. How do I also get the records that are in Request table but not in AllocationFund table?Thanks.DanYeung |
 |
|
|