SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Financial Rounding of Allocations


Financial Rounding of Allocations

Author
Message
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7211 Visits: 6431
Comments posted to this topic are about the item Financial Rounding of Allocations


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
RichB
RichB
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1553 Visits: 1058
So, er, is that not fraud...?

Pinch



Tom John-342103
Tom John-342103
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 300
If I understand the writeup correctly, the portion of the total that is not allocated to one of the contributing amounts is then allocated to just one of the records. If this is what is being done, it seems incorrect to me. I have seen plenty of situations where the unallocated portion is large enough that it is necessary to allocate the remainder across several rows, a penny at a time. Whether a cursor is needed for proper allocation or not, I do not know. However, one allocation method I find that yields good results is as follows:

For a set of records and an amount to be allocated.

Sum the records. Allocate to the first record (record amount/total_amount)*(amount_to_be_allocated) rounded to two decimal places.

Subtract the record amount from the total_amount. Subtract the amount allocated to the first record from amount_to_be_allocated.

Process each row in this manner.

On reaching the last row, the allocation is the remaining amount_to_be_allocated.
Janus Lin
Janus Lin
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 66
Thanks for sharing!

I was on a project where we needed to institue "penny rounding" in our detail tables so that the sum of the details in an invoicing detail report was consistent with various subtotals and grand totals. I specified how the rounding was to occur using Excel and pretty much followed the general approach you described in your article (do the "normal" math, sum the details, find the delta compared to the expected amount, and then apply the delta to the largest record). The developer I was working with created a stp to handle based on what I did in Excel, using the "messy" version that you described.

In a different context, where the delta was deemed significant, we also had a requirement to evenly "sprinkle" the delta across all items that were getting the allocation... IIRC I think that I only did that in Excel and we never implemented a program for that.
Misha_SQL
Misha_SQL
SSC Eights!
SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)

Group: General Forum Members
Points: 958 Visits: 1004
Awesome and just in time for the project I am working on. Thank you for a clever article!



kb1
kb1
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 122
I would be inclined to simply divide each alloc by SumOfAlloc and multiply by 100 to get a proper allocation which also adds up to the total (as long as you use a suitably precise datatype for the division).

Much simpler and can be done in a single query if you want to.

Also, dosn't allocate to zeros and spreads the unallocated amount in proportion to the existing allocations.
Tom John-342103
Tom John-342103
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 300
The problem is that, in many cases, you have to round to the nearest penny. This leads to the penny rounding problem.
Alex Fekken
Alex Fekken
SSC Veteran
SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)

Group: General Forum Members
Points: 271 Visits: 460
Yes, of course it is fraud. There is no defendable justification for the fudging (other than to cater for people who don't understand rounding).

Nice article from the "how to commit fraud if you really have to" perspective though.
Tom John-342103
Tom John-342103
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 300
Where is the fraud in penny rounding as long as it is done properly?
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7211 Visits: 6431
Thanks to all for your comments! To implement penny rounding, you simply need to change the zero (second argument of ROUND) of the "a" CTE to 2:

a AS (
SELECT d.key1, d.key2, d.Weight, d.SeqNo
,ROUND(CAST(Amount * d.Weight / w.Weight AS Money), 0) As AllocAmt
FROM d
INNER JOIN @hdr h ON h.key1 = d.key1
INNER JOIN w ON h.key1 = w.key1)



As Tom-John and Janus have noted, there are other approaches. A couple of key thoughts to keep in mind:
1. Mostly this will be used where the amounts being allocated are typically in the 1000s or 100,000s of dollars. If this allocation is performed over 100 rows, the maximum "leftover" will be 99, so the "fudged" amount represents generally less than 1% of the amount alllocated.
2. Because roughly 50% of the rows will round down and 50% will round up, in practice there will rarely be (except in contrived cases) where the leftover amount is significant.

I'm not saying this is the only way to fudge round, only that it worked for us in practice.

As to fraud, I can honestly say that I've never passed an accounting course so I'm no expert. All I know is that our accountants seemed to accept it. :-)


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search