1

I am trying to calculate commission and according to that commission value (commiss) i want to update a column value fixed_fee

 select  st1.* ,(st1.order_item_value * st1.commission_rate)/100 commiss , 
 if commiss < 50 then 
   update payment_error set fixed_fee = -5 where commiss <= 50
 else if commiss >= 50 then
     update payment_error set fixed_fee = -10 where commiss >=50
 else
   end if
 from payment_error as st1 join payment_error as st2 on st1.order_item_id=st2.order_item_id ; 

while running is then error come is :

 ERROR:  syntax error at or near "<"
 LINE 2: if commiss<50 then 
              ^

   ********** Error **********

   ERROR: syntax error at or near "<"
   SQL state: 42601
    Character: 87
1
  • 1
    Your statement is invalid beyond recognition. You would have to explain the job exactly and provide the table definition, before anyone can translate this to valid SQL properly without making many assumptions. There are pitfalls with NULL values, rounding errors, overlapping ranges, row duplications and such, aside from the invalid syntax elements. It's astounding how much fuzziness can go in such a small piece of SQL. Commented Mar 27, 2015 at 6:51

2 Answers 2

6

You can't use if in SQL. This is only available in procedural languages like PL/pgSQL.

But more importantly you cannot embed an update statement inside a select statement like that.

What you are trying to do can be done with a single UPDATE statement using a CASE:

update payment_error 
   set fixed_fee = case 
                     when (order_item_value * commission_rate)/100 <= 50 then -5
                     else 5 --<<< different value here
                   end;

You are setting the fixed_fee to -5 in both cases - I assume that is a typo, and you do mean to assign a different fee for the case when the comission is greater than 50

Sign up to request clarification or add additional context in comments.

Comments

0

You cannot use update and select together. To update you can try this:

update payment_error 
set fixed_fee = case when (order_item_value * commission_rate * .01) < 50 then -5
else 5  --In your case you have use both as -5 so change it as per your reqm 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.