在 SQL Snowflake 中将正值变为负值
turn positive values to negative in SQL Snowfalke
我有一列,其中的值描述了已退回商品的价格。它们是正的,当 sum:ing 它们时,我需要它们变成负的。
例如:
order id
item id
returned
price
quantity
123
456
True
50
1
987
123
True
10
2
下面的示例查询获取返回值的总和:
sum(case when returned = 'True' then (price * quantity) else 0 end) as returnedAmount
我的一个想法是:
sum(case when returned = 'True' then (-1*(price * quantity)) else 0 end) as returnedAmount
但是返回 null,不知道为什么。有没有人有更好的建议?
如果 returned
列是布尔值,则只比较列名:
SELECT col,
SUM(CASE WHEN retruned THEN -1*(price * quantity) ELSE 0 END) AS returnedAmmount
FROM tab
GROUP BY col;
如果查询 returns NULL,则可能意味着 PRICE 或 QUANTITY 列对于组中的所有值都可以为空:
SELECT col,
COALESCE(SUM(IIF(retruned, -1*(price * quantity),0)), 0) AS returnedAmmount
FROM tab
GROUP BY col;
所以你不需要乘以-1你可以取负值:
SELECT
order_id,
sum(iff(returned,-(price * quantity), 0)) as returnedAmount
FROM VALUES
(123,456,True,50,1),
(987,123,True,10,2)
t(order_id, item_id, returned, price,quantity)
GROUP BY 1
ORDER BY 1;
给出:
ORDER_ID
RETURNEDAMOUNT
123
-50
987
-20
所以对于 null,以太币值可能会为 null,正如 Lukasz 所展示的,您可以在总和之外修复它,有几个选项 ZEROIFNULL, COALESCE, NVL, IFNULL。
如果你想要零值,我觉得 zeroifnull 是明确的,而其他三个你必须一直向右解析表达式才能看到替代值。
SELECT
order_id,
sum(iff(returned, -(price * quantity), 0)) as ret_a,
zeroifnull(sum(iff(returned, -(price * quantity), 0))) as ret_b,
coalesce(sum(iff(returned, -(price * quantity), 0)),0) as re_c,
nvl(sum(iff(returned, -(price * quantity), 0)),0) as ret_d,
ifnull(sum(iff(returned, -(price * quantity), 0)),0) as ret_e
FROM VALUES
(123,456,True,50,1),
(987,123,True,10,2),
(988,123,True,null,2),
(989,123,True,10,null),
(989,123,True,null,null)
t(order_id, item_id, returned, price,quantity)
GROUP BY 1
ORDER BY 1;
给出:
ORDER_ID
RET_A
RET_B
RET_C
RET_D
RET_E
123
-50
-50
-50
-50
-50
987
-20
-20
-20
-20
-20
988
null
0
0
0
0
989
null
0
0
0
0
我有一列,其中的值描述了已退回商品的价格。它们是正的,当 sum:ing 它们时,我需要它们变成负的。 例如:
order id | item id | returned | price | quantity |
---|---|---|---|---|
123 | 456 | True | 50 | 1 |
987 | 123 | True | 10 | 2 |
下面的示例查询获取返回值的总和:
sum(case when returned = 'True' then (price * quantity) else 0 end) as returnedAmount
我的一个想法是:
sum(case when returned = 'True' then (-1*(price * quantity)) else 0 end) as returnedAmount
但是返回 null,不知道为什么。有没有人有更好的建议?
如果 returned
列是布尔值,则只比较列名:
SELECT col,
SUM(CASE WHEN retruned THEN -1*(price * quantity) ELSE 0 END) AS returnedAmmount
FROM tab
GROUP BY col;
如果查询 returns NULL,则可能意味着 PRICE 或 QUANTITY 列对于组中的所有值都可以为空:
SELECT col,
COALESCE(SUM(IIF(retruned, -1*(price * quantity),0)), 0) AS returnedAmmount
FROM tab
GROUP BY col;
所以你不需要乘以-1你可以取负值:
SELECT
order_id,
sum(iff(returned,-(price * quantity), 0)) as returnedAmount
FROM VALUES
(123,456,True,50,1),
(987,123,True,10,2)
t(order_id, item_id, returned, price,quantity)
GROUP BY 1
ORDER BY 1;
给出:
ORDER_ID | RETURNEDAMOUNT |
---|---|
123 | -50 |
987 | -20 |
所以对于 null,以太币值可能会为 null,正如 Lukasz 所展示的,您可以在总和之外修复它,有几个选项 ZEROIFNULL, COALESCE, NVL, IFNULL。
如果你想要零值,我觉得 zeroifnull 是明确的,而其他三个你必须一直向右解析表达式才能看到替代值。
SELECT
order_id,
sum(iff(returned, -(price * quantity), 0)) as ret_a,
zeroifnull(sum(iff(returned, -(price * quantity), 0))) as ret_b,
coalesce(sum(iff(returned, -(price * quantity), 0)),0) as re_c,
nvl(sum(iff(returned, -(price * quantity), 0)),0) as ret_d,
ifnull(sum(iff(returned, -(price * quantity), 0)),0) as ret_e
FROM VALUES
(123,456,True,50,1),
(987,123,True,10,2),
(988,123,True,null,2),
(989,123,True,10,null),
(989,123,True,null,null)
t(order_id, item_id, returned, price,quantity)
GROUP BY 1
ORDER BY 1;
给出:
ORDER_ID | RET_A | RET_B | RET_C | RET_D | RET_E |
---|---|---|---|---|---|
123 | -50 | -50 | -50 | -50 | -50 |
987 | -20 | -20 | -20 | -20 | -20 |
988 | null | 0 | 0 | 0 | 0 |
989 | null | 0 | 0 | 0 | 0 |