根据另一列将值更新为负数
Update values to negative based on another column
我有一个 table,其中包含销售发票和贷方通知单,两者都作为正值保存在 table 中。
我想 select 数据并在 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
来自 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
非常感谢,我玩过 ABS,但我无法弄清楚获得所需输出的确切用法。
我假设当 type = 1 时为正,当 type = 2 时为负。如果该类型可以包含更多值,则您将不得不修改 case 语句,因为除 1 之外的所有值都是负数。
select
key
, description
, type
, case
when type = 1 then amount
else
amount * -1
end as amount
另一种选择是choose()
例子
Select [Key]
,[Description]
,[Type]
,[Amount] = [Amount]*choose([type],1,-1)
From YourTable
我有一个 table,其中包含销售发票和贷方通知单,两者都作为正值保存在 table 中。
我想 select 数据并在 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
来自 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
非常感谢,我玩过 ABS,但我无法弄清楚获得所需输出的确切用法。
我假设当 type = 1 时为正,当 type = 2 时为负。如果该类型可以包含更多值,则您将不得不修改 case 语句,因为除 1 之外的所有值都是负数。
select
key
, description
, type
, case
when type = 1 then amount
else
amount * -1
end as amount
另一种选择是choose()
例子
Select [Key]
,[Description]
,[Type]
,[Amount] = [Amount]*choose([type],1,-1)
From YourTable