inserting if in a select query
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.
精彩评论