根据另一列将值更新为负数

问题描述 投票:-1回答:2

我有一个包含销售发票和贷方通知单的表,它们都作为正值保存在该表中。

我想选择数据,并在select语句中根据另一列将贷项凭证值更改为负数。

有什么想法吗?

已添加信息更新

Key          Description            Type          Amount
--------------------------------------------------------
1            Invoice for 345         1             200
2            Invoice for 346         1             250
3            Invoice for 347         1             280
4            Credit  for 347         2             200
5            Invoice for 345         1             400
6            Credit note zz665       2             380

选择所需的结果


Key          Description            Type          Amount
--------------------------------------------------------
1            Invoice for 345         1             200
2            Invoice for 346         1             250
3            Invoice for 347         1             280
4            Credit  for 347         2            -200
5            Invoice for 345         1             400
6            Credit note zz665       2            -380

非常感谢,我一直在使用ABS,但是我无法弄清楚确切的用法来获得所需的输出。

sql sql-server select negative-number
2个回答
0
投票

我假设当类型= 1时为正,而当类型= 2时为负。如果类型可以包含更多值,则您将不得不修改case语句,因为除1以外的所有内容均为负数。

select
  key
, description
, type
, case
   when type = 1 then amount
  else
   amount * -1
  end as amount

0
投票

另一个选择是choose()

示例

Select [Key] 
      ,[Description]
      ,[Type]
      ,[Amount] = [Amount]*choose([type],1,-1)
 From  YourTable
© www.soinside.com 2019 - 2024. All rights reserved.