Solved

Tiered commission calculation in SQL - How to calculate?

Posted on 2016-09-05
4
61 Views
Last Modified: 2016-09-06
I'm having the exact same problem as poster: tmyint had a few years back (a good few years)
View the original post and solution of poster: tmyint.

I trying to understand what the .01 in Scotts solution refers to, does any one know why in the CASE statement, inside the accepted solution he uses this "THEN .01"

I'll paste the exact code, in for quicker reference than the link

--create sample/test data
DROP TABLE commissions
CREATE TABLE commissions (
    tier TINYINT,
      flag TINYINT,
      lowValue DECIMAL(12, 2),
      highValue DECIMAL(12, 2),
      pct DECIMAL(3, 2)
      )
INSERT INTO commissions VALUES(0, 0,      0,     2147483647,  0.50) --default commission
INSERT INTO commissions VALUES(1, 1,      0,         100000,  0.50)
INSERT INTO commissions VALUES(2, 1, 100000.01,      250000,  0.55)
INSERT INTO commissions VALUES(3, 1, 250000.01,  2147483647,  0.60)

DROP TABLE revenues
CREATE TABLE revenues (
      empNum INT,
      gross DECIMAL(12, 2),      
      commissionFlag TINYINT
      )
INSERT INTO revenues VALUES(1111111, 300500, 1)
INSERT INTO revenues VALUES(2222222, 300500, 0)


SELECT rev.empNum, rev.gross, 
      SUM(CASE WHEN rev.gross > com.highValue 
               THEN com.highValue - com.lowValue + CASE WHEN com.lowValue <> 0 THEN .01 ELSE 0 END
               ELSE rev.gross - com.lowValue + CASE WHEN com.lowValue <> 0 THEN .01 ELSE 0 END END * com.pct) 
      AS [commission]
FROM revenues rev
INNER JOIN commissions com ON rev.commissionFlag = com.flag AND
      rev.gross >= com.lowValue
GROUP BY rev.empNum, rev.gross

Open in new window

0
Comment
Question by:fadiel ras
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
I believe Scott is adding up percentages.
I.e.
Sum (.01)
Results in the total percentage to apply


Quite crafty actually.
0
 

Author Comment

by:fadiel ras
Comment Utility
Thanks Paul. I'm not exactly following, but let me check in more detail and revert asap
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
Try runnning the query without the sum () or group by so that you get to see the generated rows before the sum is performed.

This should help you understandbthe logic.
0
 

Author Closing Comment

by:fadiel ras
Comment Utility
Thanks Paul, I get it now, appreciate the fast response.
PS - I'm posting a related, but different question in a few minutes, hopefully you'll again be able to help, thank you
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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

9 Experts available now in Live!

Get 1:1 Help Now