Avatar of fadiel ras
fadiel ras

asked on 

using a variable within a CASE statement

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
User generated image
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
fadiel ras
Avatar of Steven Kribbe
Steven Kribbe
Flag of Netherlands image

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?
Avatar of Guy Hengel [angelIII / a3]
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
Avatar of fadiel ras
fadiel ras

ASKER

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?
Avatar of fadiel ras
fadiel ras

ASKER

Thanks Guy. I tried the CAST option, it still returned zero's. let me give Steven's a try and advise.
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of fadiel ras
fadiel ras

ASKER

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?
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of fadiel ras
fadiel ras

ASKER

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!
User generated image
Avatar of fadiel ras
fadiel ras

ASKER

my apologies everyone. I picked an error in my code! I'll post it after this, then close. very sorry.
ASKER CERTIFIED SOLUTION
Avatar of fadiel ras
fadiel ras

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of fadiel ras
fadiel ras

ASKER

Thanks all
Avatar of fadiel ras
fadiel ras

ASKER

error made with my original post. corrected in the final post.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo