how do I subtract values from two select statements

后端 未结 4 971
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 19:11

I would like to subtract one value from another value. The schema of the table is as follows:

   tag, datetime,value
   ------------
   tag1, 2010-1-1 10:10         


        
4条回答
  •  感情败类
    2021-01-19 20:09

    Do you really need wrapping select statement?

    You can declare two variables @value1 and @value2 and substruct them.

    declare @value1 int, @value2 int
    
    select @value1 = Max(Value) as [Value1] from History WHERE Datetime ='2010-1-1 10:10'       and tagname ='tag1'
    
    select @value2 = Max(Value) as [Value2] from History WHERE Datetime ='2010-1-1 10:12'      and Tagname ='tag2'
    
    select @value1 - @value2
    

提交回复
热议问题