--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
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
This award recognizes a member of Experts Exchange who has made outstanding contributions to the community within their first year as an expert. The Rookie of the Year is awarded to a new expert who has the highest number of quality contributions.