我试图做出基于ID从表一显示,从表P具有相同TimestampLocal立柱广告表A.最接近行查询T-SQL查询与接近的时间戳选择值
我已经成功地做到:
IF(A.Generated - P.Local) >= 0 THEN
select P.Location where P.Local = A.Generated + MIN(A.Generated - P.Local)
ELSE
select P.Location where P.Local = A.Generated + MAX(A.Generated - P.Local)
我想从表中的值P.
IF (select datediff(second, a.Generated,p.Local) from A a inner join P p on a.VId=p.VId where a.Id = 830566) >=0
select Location from P where Local = dateadd(millisecond,(select Min(datediff(second, a.Generated,p.Local)) from A a inner join P p on a.VId=p.VId where a.Id=830566),
(select Generated from A where Id=830566))
ELSE
select Location from P where Local =dateadd(second,(select Max(datediff(millisecond, a.Generated,p.Local)) from A a inner join P p on a.VId=p.VId where a.Id=830566),
(select Generated from A where Id=830566))
但我得到的错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
和
The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.
但我不知道为什么:(这是可以基于表格中的编号从表P得到的只是最近的行?如果有多个来自P的同一个TimeStampLocal的行只能得到第一个?
UPDATE
表A:
Id Generated VId
830566 | 2017-06-04 10:38:22.2000000 -07:00 | 5635
830567 | 2017-06-04 10:38:45.1000000 -07:00 | 5634
830568 | 2017-06-04 10:31:59.6000000 -07:00 | 5638
表P中:
VId Local Location
5638 | 2017-06-04 10:26:17.9000000 -07:00 | 0xE6
5638 | 2017-06-04 10:31:48.6000000 -07:00 | 0X7F
5638 | 2017-06-04 10:32:48.7000000 -07:00 | 0x3C
5634 | 2017-06-04 10:31:48.6000000 -07:00 | 0xA6
作为例子:标识830568
从表A(接收作为参数) - 识别符5638
,我应该从距离表P最近的当地时间和相同的VId,在这种情况下是第二个2017-06-04 10:31:48.6000000 -07:00
,对于P从这一行我需要得到的位置:0X7F
你可以发布一些样本数据(作为插入到...)和期望值(作为文本)? – etsa
@etsa我已经做了必要的更新 – Dana
最近没有超过或最近期?如果VId 5638的10:32:00应该退还还是您所说的价值? – scsimon