Solved

using a variable within a CASE statement

Posted on 2016-09-06
12
29 Views
Last Modified: 2016-09-11
Hi there,
I have a fairly simple CASE statement.
In this query I derive at my commission value based on calculated values from tblCommissionTiers .
The query works well for the tiers below 100%. I'm now trying to use a variable @MaxComm to pass the highest CommissionPercentage to the same calculation as the lower values for instances when Target is exceeded.
in other words, whenever gross is more than targ.SalesTarget, I would like to dynamically apply the highest CommissionPercentage.
Can anyone assist me with using paramtres inside a CASE statement please?

here is my script

DECLARE @MaxComm decimal (3,2)
SET @MaxComm = ( select max(CommissionPercentage) from tblCommissionTiers)

SELECT DISTINCT empNum, gross as SALES ,targ.SalesTarget AS TARGET
				, lowPercentage,highPercentage, CommissionPercentage
				,gross*lowPercentage AS LowAmount
				,gross*highPercentage AS HighAmount
				,(gross*highPercentage) - (gross*lowPercentage) VarHighVsLow
				,CASE WHEN gross > targ.SalesTarget
						THEN ((gross*highPercentage) - (gross*lowPercentage)) * CommissionPercentage
					  WHEN (gross - targ.SalesTarget) > 100.00
						THEN (gross - targ.SalesTarget) * @MaxComm
					ELSE 0   END COMMISSION
FROM tblRevenues rev
  INNER JOIN tblCommissionTiers com 
	ON rev.commissionFlag = com.commissionFlag 
	 INNER JOIN [dbo].[tblTargets] Targ
	  ON com.commissionFlag  = targ.commissionFlag
GROUP BY empNum, gross,targ.SalesTarget , lowPercentage ,highPercentage, CommissionPercentage

Open in new window


My concern is the zero output in the COMMISSION column

here are my output values
my SQL output
0
Comment
Question by:fadiel ras
  • 8
  • 2
  • 2
12 Comments
 
LVL 4

Expert Comment

by:Steven Kribbe
ID: 41785915
I think the problem with the zero line is the fact that the first WHEN clause is true, calculating the high - low which is zero. I would expect the calculation to be this:

CASE WHEN gross <= targ.SalesTarget
                  THEN ((gross*highPercentage) - (gross*lowPercentage)) * CommissionPercentage
       WHEN (gross > targ.SalesTarget)
                  THEN (gross - targ.SalesTarget) * @MaxComm
            ELSE 0   END COMMISSION

Or do you want to calculate seperate commission for reaching the target AND max commission for only the part that was above target?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41785924
I rather see that SalesTarget is an integer, which will result the entire result to be computed as integer..

please try this:
replace:
THEN (gross - targ.SalesTarget) * @MaxComm
by this:
THEN ( gross - cast ( targ.SalesTarget as decimal(10,4))  ) * @MaxComm
0
 

Author Comment

by:fadiel ras
ID: 41785925
Thanks Steven. I'll try your CASE asap.
>>Or do you want to calculate separate commission for reaching the target AND max commission for only the part that was above target?
I'm trying to use the variance between the Sales and Target as the base, to then apply the Max commission percentage to that variance...hope that makes sense?
0
 

Author Comment

by:fadiel ras
ID: 41785929
Thanks Guy. I tried the CAST option, it still returned zero's. let me give Steven's a try and advise.
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 41785934
actually, I see that:
WHEN (gross - targ.SalesTarget) > 100.00

for gross = 105, and Target = 100, this will not match, hence it goes to the "else"
hence, Steve is right with  his suggestion
0
 

Author Comment

by:fadiel ras
ID: 41785950
Hi Steven, it's also not that.
I cant change the first condition to less than Target, as their are different tiers ( you can view from the embedded image on my original post)
so I'm hoping to achieve something like an ELSE- if I could simply use the MAX() in the ELSE then that would work perfectly. So I'm thinking to apply the CASE in my calc, then somehow say, everything ELSE may be at the MAX() percentage.
any ideas how to achieve this?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Assisted Solution

by:Steven Kribbe
Steven Kribbe earned 250 total points
ID: 41785982
maybe a nice approach would be defining a different column in the query for each tier of commission and one column that adds them all up. I still think you can make a different calculation in case of reaching the target or performing better than the target.
I am still a bit confused about the variation bit.
0
 

Author Comment

by:fadiel ras
ID: 41785996
that's exactly what I've just started doing, as I need to understand that I'm doing it correctly (for my own sanity)
if you can help think of another way, then I'd be grateful, it would be much cleaner than the individual columns.

I've inserted an image that might explain my intent better

thanks thus far!
Commission Calc
0
 

Author Comment

by:fadiel ras
ID: 41786007
my apologies everyone. I picked an error in my code! I'll post it after this, then close. very sorry.
0
 

Accepted Solution

by:
fadiel ras earned 0 total points
ID: 41786023
I was passing incorrect logic to my query
The calc should take the gross (sales) less SalesTarget (refer to ROW NR 3 and 13 respectively)
then multiply that with the @MaxComm.
again, my apologies, but thanks for leading to trace my error.

--INCORRECT QUERY
CASE	
  WHEN gross <= targ.SalesTarget
   THEN (gross-targ.SalesTarget) * CommissionPercentage
 WHEN (gross > targ.SalesTarget)
   THEN (gross - targ.SalesTarget) * @MaxComm
    ELSE 0   END COMMISSION


--CORRECT QUERY
CASE 
 WHEN gross <= targ.SalesTarget
  THEN ((gross*highPercentage) - (gross*lowPercentage)) * CommissionPercentage
 WHEN (gross > targ.SalesTarget)
  THEN (gross - targ.SalesTarget) * @MaxComm
   ELSE 0   END COMMISSION

Open in new window

0
 

Author Comment

by:fadiel ras
ID: 41786024
Thanks all
0
 

Author Closing Comment

by:fadiel ras
ID: 41793236
error made with my original post. corrected in the final post.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

14 Experts available now in Live!

Get 1:1 Help Now