开发者

inserting if in a select query

开发者 https://www.devze.com 2022-12-17 00:52 出处:网络
How can i do something like this ? SELECT(IF(SUM(Costo) > 0) 开发者_如何学JAVASUM(Costo) ELSE 0) AS Expr1

How can i do something like this ?

SELECT    (IF(SUM(Costo) > 0) 开发者_如何学JAVASUM(Costo) ELSE 0) AS Expr1
FROM         TDP_NotaSpeseSezB


You want to use a CASE statement.

Select 
  CASE WHEN SUM(Costo) > 0 THEN SUM(Costo) 
  ELSE 0 
  END 'Expr1'
FROM
  TDP_NotaSpeseSezB


You can use case statement like this:

SELECT case when sum(Costo)> 0 then sum(Costo) 
       else 0 end as Expr1 
FROM TDP_NotaSpeseSezB

CASE (Transact-SQL)


SELECT   CASE WHEN SUM(Costo) > 0 THEN SUM(Costo) ELSE 0 END AS Expr1
FROM     TDP_NotaSpeseSezB

Other major engines support this syntax:

SELECT  GREATEST(SUM(Costo), 0)
FROM     TDP_NotaSpeseSezB

SQL Server, however, does not.

0

精彩评论

暂无评论...
验证码 换一张
取 消