Madan Madan
Madan Madan

Reputation: 684

Correct Sql Script for Formula

Can anyone help me write SQL script for the following formula?

If DEP = 1
   If DROP 1
      PLV = 334.86 * exp(0.3541 * ACTIVE_DAYS) + 0.25 * DROP + 20 * DEP
   Else
      If DROP < 0
         PLV = DROP + 70 * ACTIVE_DAYS
      Else
         PLV = 0.25 * DROP + 70 * ACTIVE_DAYS

The SQL script which I have is the following

 SELECT IF(dep=1, if(dep=1, (334.86 * exp(0.3541 * act_days)) + 
 (0.25 * 'drop') + (20 *   dep),
 if('drop'<0, 'drop' + (70 * act_days), (0.25 * 'drop') + (70 * act_days))),'0')
 as PLV

But the above query is not right as something is missing where the formula says

Else
PLV = 0.26 * DROP

Thanks,

Upvotes: 2

Views: 79

Answers (1)

Dave Sexton
Dave Sexton

Reputation: 11188

How about this:

CASE "DEP" 
  WHEN 1 
  THEN CASE  
         WHEN "DROP" = 1 THEN 334.86 * exp(0.3541 * "ACTIVE_DAYS") + 0.25 * "DROP" + 20 * "DEP"
         WHEN "DROP" < 0 THEN "DROP" + 70 * "ACTIVE_DAYS"
         ELSE 0.25 * "DROP" + 70 * "ACTIVE_DAYS"
         END
  END

Upvotes: 1

Related Questions