Question

In my stored procedure i am now trying to add FEE to my sales.

  • If the item_price is > 69 it should have 6 in fee
  • If the item_price is <= 69 it should have 3 in fee
  • If the item_price is <= 19 it should have fee 1

I have tried adding it to my SELECT statement, but i dont know how to insert it properly.

   AS 'Income inc VAT, ex Discount',
   case when item_price > 69 then 6
   case when item_price <= 69 then 3
   else 1
   AS 'fee'

To give an example on how im trying to implement it, i have added some of the select statement...

Solution: Inside my select statement i added this code based on the great answers i got, and it worked:

       case when item_price > 69 then 6 
       when item_price <= 69 then 3 
       else 1
       end
       AS 'Fee'
Était-ce utile?

La solution

You should use case statment:

Writing from memory:

insert into fee (fee_value)
select 
  case when item_price > 69 then 6
  case when item_price <= 69 then 3
  else 1
end

And so on...

Or, maybe you want to use variable:

declare @fee int

if @item_price > 69 
 set @fee = 6

...

insert into fee(fee_value) 
values (@fee)

Or another way:

declare @item_price int  = 12

declare @fee int

SELECT
  @fee = case 
           when @item_price > 69 then 6
           when @item_price between 13 and 69 then 2
           else 1
         end

select @fee   
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top