2017-06-06 88 views
1

我试图做出基于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

+0

你可以发布一些样本数据(作为插入到...)和期望值(作为文本)? – etsa

+0

@etsa我已经做了必要的更新 – Dana

+0

最近没有超过或最近期?如果VId 5638的10:32:00应该退还还是您所说的价值? – scsimon

回答

1

这应该这样做。如果您想要最近的时间没有“超过”,请删除围绕datediff()函数的ABS(),并取消注释连接条件。

declare @TableA table (Id int, [Generated] datetime2, VId int) 

declare @TableB table (VId int, [Local] datetime2, [Location] varchar(64)) 

insert into @TableA 
values 
(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) 

insert into @TableB values 

(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') 


;with cte as(
select 
    a.Id 
    ,a.VId 
    ,a.Generated 
    ,b.Local 
    ,b.Location 
    ,ABS(DATEDIFF(second,a.Generated,b.Local)) as TD 
from 
    @TableA a 
inner join 
    @TableB b on 
    b.VId = a.VId) 
    --and b.local < a.Generated 

select 
    c.Id 
    ,c.VId 
    ,c.Location 
from 
    cte c 
    inner join 
     (select ID, min(TD) TD 
     from cte 
     group by ID) c2 on c2.Id = c.Id and c2.TD = c.TD 
+0

完美的作品!非常感谢您的回答! :)起来! – Dana

+0

无汗@Dana祝你好运 – scsimon

+0

你也是! :)再次感谢! – Dana