Home > Software design >  Return Negative values on SQL
Return Negative values on SQL

Time:09-14

I have this data in postgreSQL

Id   type      quantity
1    order       10
2    order       12
3    order       11
4    purchase    5
5    purchase    4
6    credit      2

I would like to return the quantity negative when the type = 'order' or 'credit

Id   type      quantity
1    order       -10
2    order       -12
3    order       -11
4    purchase     5
5    purchase     4
6    credit      -2

How do i do this in postgresql?

CodePudding user response:

you can do it with the case statement

select Id,type,case when type in ('order','credit') then quantity*-1 else quantity end as quantity 
  from tableName

if the quantity is already having the negative value then you have to add another case statement.

  • Related