我有一个包含带时间戳数据的表。数据值是在不同的参考系中测量的,因此在下面的例子中,A由B测量,但是B由C测量。我可以通过查找A的值来估计如果用C测量的A将是什么。 > B和B-> C用于相同的时间戳,并将它们加在一起。我一直在尝试编写一个查询,该查询采用每个A-> B测量,查找最接近的B-> C测量,并返回TimestampA,ValueA,TimestampB,ValueB,Value A + B.有人可以帮忙吗?
TimeStamp ID ParentID Value
19:44:04.877 A B 176.5429
19:44:04.877 A B 176.2653
19:44:09.893 A B 170.5266
19:44:09.893 A B 170.883
19:44:14.887 A B 164.3122
19:44:14.887 A B 164.1429
19:44:19.880 A B 158.5632
19:44:19.880 A B 158.1787
19:44:24.870 A B 153.0709
19:44:24.870 A B 152.8471
19:44:00.403 B C 195184.2
19:44:05.443 B C 195189.8
19:44:10.450 B C 195195.4
19:44:15.487 B C 195201.3
19:44:20.523 B C 195207.2
19:44:25.570 B C 195212.6
答案 0 :(得分:1)
你可以自己LEFT JOIN
表,看看:
SELECT this.TimeStamp AS [this TimeStamp],
this.Value AS [this Value],
parent.Timestamp AS [parent Timestamp],
parent.Value AS [parent Value],
this.Value + ISNULL(parent.Value, 0) AS [Total Value]
FROM dbo.TableName this
LEFT OUTER JOIN dbo.TableName parent
ON this.ParentID = parent.ID
AND parent.Value = (SELECT MIN(Value)FROM dbo.TableName p
WHERE p.ID = this.ParentID
AND p.Value >= this.Value)