Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 条件数学方程_Sql Server_Tsql_Sql Server 2000 - Fatal编程技术网

Sql server 条件数学方程

Sql server 条件数学方程,sql-server,tsql,sql-server-2000,Sql Server,Tsql,Sql Server 2000,我需要在select查询期间执行一些求和,但是根据2个值,我需要执行不同的等式。希望有一个例子能说明这一点 基本上,我需要进行以下总结 if x > y then (x - y + z) or if x < y then (x - x + z) basically i am setting this to 0. 到目前为止,我认为我可以使用两个表来转储x>y值和x

我需要在select查询期间执行一些求和,但是根据2个值,我需要执行不同的等式。希望有一个例子能说明这一点

基本上,我需要进行以下总结

if x > y then (x - y + z) or
if x < y then (x - x + z) basically i am setting this to 0.
到目前为止,我认为我可以使用两个表来转储x>y值和x
任何想法都可以使用case表达式

select case 
         when x > y then x - y + z
         when x < y then x - x + z
         else 0 -- x = y
       end  
from YourTable

可以使用大小写表达式

select case 
         when x > y then x - y + z
         when x < y then x - x + z
         else 0 -- x = y
       end  
from YourTable

看起来这里有3个值。。。你能详细说明一下你所需要的表格和结果吗?看起来你这里有3个值。。。你能详细说明你所需要的表格和结果吗?从技术上讲,这是一个case表达式,而不是case语句。@Damien_the_unsiver-当然可以。谢谢我在英语方面有点困难,但T-SQL几乎很流利:。从技术上讲,这是一个case表达式,而不是case语句。@Damien_,不信者-当然是。谢谢我在英语方面有点困难,但T-SQL几乎很流利:。