gpt4 book ai didi

mysql - SQL 除以 SELECT 语句中的零错误

转载 作者:可可西里 更新时间:2023-11-01 07:56:10 25 4
gpt4 key购买 nike

我是 SQL 的新手,需要帮助。我的 SELECT 语句中出现以下除零错误:

SUM(Cast((replace(replace(replace (p.[Total Sales], '$', ''), '(','-'),  ')','')) as money)) - SUM(Cast((replace(replace(replace (p.[Total Cost], '$', ''), '(','-'),  ')','')) as money)) / SUM(Cast((replace(replace(replace (p.[Total Sales], '$', ''), '(','-'),  ')','')) as money)) as new_bal

我知道我需要使用 CASE,但我不确定应用程序。

最佳答案

你可以使用 NULLIF 在以下内容中:

NULLIF(expression1, 0)

在你的情况下它将是:

SUM(Cast((replace(replace(replace (p.[Total Sales], '$', ''), '(','-'),  ')','')) as money)) - SUM(Cast((replace(replace(replace (p.[Total Cost], '$', ''), '(','-'),  ')','')) as money)) / NULLIF(SUM(Cast((replace(replace(replace (p.[Total Sales], '$', ''), '(','-'),  ')','')) as money)),0) as new_bal

关于mysql - SQL 除以 SELECT 语句中的零错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30757640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com