在 SQL Snowfalke 中将正值转为负值

发布于 2025-01-15 02:55:41 字数 1083 浏览 2 评论 0原文

我有一列,其中的值描述已退回商品的价格。它们是积极的,当对它们求和时,我需要它们变成消极的。 例如:

订单 id商品 id返回价格数量
123456True501
987123True102

下面的示例查询获取返回值的总和:

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,不知道为什么。有人有更明智的建议吗?

I have a column where the values describe the price of an item that has been returned. They are positive and when sum:ing them I would need them to become negative.
Ex:

order iditem idreturnedpricequantity
123456True501
987123True102

Example query below to get the sum of the returned value:

sum(case when returned = 'True' then (price * quantity) else 0 end) as returnedAmount

One thought I had was:

sum(case when returned = 'True' then (-1*(price * quantity)) else 0 end) as returnedAmount

But that returned null, not sure why. Does anyone have a smarter suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中二柚 2025-01-22 02:55:41

如果返回列是布尔值,则比较只是列名称:

SELECT col, 
  SUM(CASE WHEN retruned THEN -1*(price * quantity) ELSE 0 END) AS returnedAmmount 
FROM tab 
GROUP BY col;

如果查询返回 NULL,则可能意味着 PRICE 或 QUANTITY 列对于组中的所有值都可为空:

SELECT col, 
  COALESCE(SUM(IIF(retruned, -1*(price * quantity),0)), 0) AS returnedAmmount 
FROM tab 
GROUP BY col;

If the returned column is boolean then comparison is just column name:

SELECT col, 
  SUM(CASE WHEN retruned THEN -1*(price * quantity) ELSE 0 END) AS returnedAmmount 
FROM tab 
GROUP BY col;

If the query returns NULL it could mean that either PRICE or QUANTITY columsn are nullable for all values in a group:

SELECT col, 
  COALESCE(SUM(IIF(retruned, -1*(price * quantity),0)), 0) AS returnedAmmount 
FROM tab 
GROUP BY col;
笑脸一如从前 2025-01-22 02:55:41

所以你不需要乘以-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_IDRETURNEDAMOUNT
123-50
987-20

所以到空,所以以太值可以为空,正如卢卡斯所示,你可以在外面修复它总而言之,有几个选项 ZEROIFNULLCOALESCE,< a href="https://docs.snowflake.com/en/sql-reference/functions/nvl.html" rel="nofollow noreferrer">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_IDRET_ARET_BRET_CRET_DRET_E
123-50-50-50-50-50
987-20-20-20-20-20
988null0000
989null0000

so you don't need to multiply by -1 you can just negate the value:

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;

gives:

ORDER_IDRETURNEDAMOUNT
123-50
987-20

So to the null, so ether value could null and as Lukasz showed, you can fix that on the outside of the sum, there are a few options ZEROIFNULL, COALESCE, NVL, IFNULL.

if you want the value zero, I feel zeroifnull is explicit, while the other three you have to parse the expression all the way to the right to see the alternative value.

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;

gives:

ORDER_IDRET_ARET_BRET_CRET_DRET_E
123-50-50-50-50-50
987-20-20-20-20-20
988null0000
989null0000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文