gpt4 book ai didi

sql - 将 Avg 值除以零时取 0

转载 作者:行者123 更新时间:2023-12-02 06:27:47 25 4
gpt4 key购买 nike

我有两个数据库字段 (SQL Server),这些字段是十进制的。这两个字段在 SQL 查询中都是 0 我需要取这些值的百分比。在此记录中显示数据库错误。但如果发生这种情况,我需要显示 0 值。

There's a scenario TotalSalesPrice = 0.00 & CurrNewsPrice= 0 Then the Avgnews formular is AvgNews = (CurrNewsPrice / TotalSalesPrice) * 100

Select  TotalSalesPrice,CurrNewsPrice,CAST(CurrNewsPrice AS decimal(18,2)) / CAST(TotalSalesPrice AS decimal(18,2)) * 100.00 AS AvgNews
from [dbo].[N_NewsLine]

最佳答案

特例吗?

select  TotalSalesPrice,CurrNewsPrice, case
when TotalSalesPrice = 0 and CurrNewsPrice = 0 then 0
else CAST(CurrNewsPrice AS decimal(18,2)) /
CAST(TotalSalesPrice AS decimal(18,2)) * 100.00
end AS AvgNews
from [dbo].[N_NewsLine]

关于sql - 将 Avg 值除以零时取 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782056/

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