我如何将此列中的负值替换为0

时间:2019-04-05 05:55:08

标签: sql-server

SUM([X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)]) AS DAmount

我如何在尝试的此列中将负值替换为0

select sUM(IF ([X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)]  < 0, 0, X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)] )) as DAmount

4 个答案:

答案 0 :(得分:1)

您可以在以下情况下使用案例

select sum(case when [X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)] >0 
  then [X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)]  end ) as DAmount

答案 1 :(得分:0)

使用case表达式:

(CASE WHEN SUM([X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)]) > 0
      THEN SUM([X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)])
      ELSE 0
 END) AS DAmount

答案 2 :(得分:0)

用例何时

select case when SUM([X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)])<0 then 0 else SUM([X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)]) end
  AS DAmount

答案 3 :(得分:0)

我注意到用case语句有多个答案,但是还有另一个可能使您感兴趣的选项(使用where子句)。

Select SUM([X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)]) AS DAmount
FROM Table
Where [X Limited$Detailed Cust_ Ledg_ Entry].[Amount (LCY)]>0