tsql条件数学方程

Ric*_*nks 0 t-sql sql-server sql-server-2000

我需要在选择查询期间执行一些求和,但是根据我需要执行不同等式的2个值.希望一个例子可以证明

基本上我需要执行以下总结

if x > y then (x - y + z) or
if x < y then (x - x + z) basically i am setting this to 0.
Run Code Online (Sandbox Code Playgroud)

到目前为止,我认为我可以使用2个表来转储x> y值和x <y值,然后执行相关的方程式.

有任何想法吗

Mik*_*son 5

您可以使用案例表达式.

select case 
         when x > y then x - y + z
         when x < y then x - x + z
         else 0 -- x = y
       end  
from YourTable
Run Code Online (Sandbox Code Playgroud)