我想从另一个值中减去一个值。该表的架构如下:
tag, datetime,value
------------
tag1, 2010-1-1 10:10:00, 123
tag2, 2010-2-2 10:12:00. 321
select * from
( (Select Max(Value) as [Value1] from History WHERE Datetime ='2010-1-1 10:10' and tagname ='tag1') as v1 -
( (Select Max(Value) as [Value2] from History WHERE Datetime ='2010-1-1 10:12' and Tagname ='tag2') as v2))
显然我迷路了...我该怎么做。
感谢
MS-SQL
答案 0 :(得分:8)
总猜:
select v1.Value1 - v2.Value2 from
(Select Max(Value) as [Value1] from History WHERE Datetime ='2010-1-1 10:10' and tagname ='tag1') as v1
CROSS JOIN
( (Select Max(Value) as [Value2] from History WHERE Datetime ='2010-1-1 10:12' and Tagname ='tag2') as v2)
答案 1 :(得分:1)
你真的需要包装select语句吗?
您可以声明两个变量@value1
和@value2
并对其进行子结构化。
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
答案 2 :(得分:0)
值列的类型是什么?如果它已经是整数,那就做:
SELECT
(Select Max(Value) as [Value1] from History WHERE Datetime ='2010-1-1 10:10' and tagname ='tag1') as v1 -
(Select Max(Value) as [Value2] from History WHERE Datetime ='2010-1-1 10:12' and Tagname ='tag2') as v2
否则你必须将它转换为整数或任何你想要的数字类型
答案 3 :(得分:0)
select h1.value - h2.value
from History h1
inner join History h2 on h1.Datetime = '2010-1-1 10:10' and Datetime ='2010-1-1 10:12'