Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

Distrubuting a value

$
0
0

This question has to do with finding the right approach to a math problem.

I have a amount of fuel that I need to be reimbursed for.  The amount for last week was $10,000.  I need to distribute that amount across the total earnings of a truck as a percentage of the total of all trucks in the fleet.

Truck #1 made $20,000

Truck #2 made $34,000

Truck #3 made $14,000

Truck #4 made $44,000
All trucks total $112,000.  So we calculate a percentage  $10,000 / $112,000 * 100 = 8.93% (rounded up to nearest hundredth)
Now we apply that percentage to each truck so that we can take our reimbursement evenly.
20,000 * 0.0893 = 1,786.00
34,000 * 0.0893 = 3,036.20
14,000 * 0.0893 = 1,250.20
44,000 * 0.0893 =  3,929.20
Reimbursement totals $10,001.6.  $1.60 over which is fine, we're made whole on our distribution with reasonable accuracy.
My problem is that given the above scenario when I apply it to a few thousand records that I must first aggregate with sum() and calculate a grand total essentially a sum(sum()) for the percentage I'm off by ~$2,000 left to distribute.  All values are stored in SQL as decimal(16,2).  I am using some rounding to trim off the excess with the percentage calculation.
At first I suspected rounding so I took the round() out of play in my SQL hoping that I'd have a higher then needed distribution.  Turns out that didn't change much.  Somehow I must not be using a good strategy to apply my distribution.
So with all that said, what is your favorite method in SQL to apply a distribution over a large data set?  Also, how would you calculate down to the penny?

I'm writing this in T-SQL by the way...

Thanks!


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>