Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Complicated logic

Author  Topic 

divan
Posting Yak Master

153 Posts

Posted - 2012-08-21 : 08:40:29
I have two type of policies (insurance policies) one that start with a T and one that start with a M

example T10000 and M300000

for policies that start with a T I have to calculate the following discounts

discount_1
discount_2
discount_3

for policies that start with M I have to calculate the above discounts plus additional discounts such as

discount_1
discount_2
discount_3
discount_9
discount_10
discount_11

what would be the most efficient way to write the script such that it covers both policies and without having to repeat the code...

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-21 : 08:46:45
Assuming discounts are given as a fraction, something like this:
SELECT
UndiscountedPremium,
UndiscountedPremium
* (1.0-discount_1)
* (1.0-discount_2)
* (1.0-discount_3)
* CASE
WHEN PolicyNumber LIKE 'M%' THEN
(1.0-discount_9)
* (1.0-discount_10)
* (1.0-discount_11)
ELSE 1.0
END
FROM
TheTable
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-21 : 10:12:36
if discount fields are NULLABLE make sure you wrap them within COALESCE or ISNULL to make NULLs 0 otherwise it will cause entire results to be NULL

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -