2017-06-06 45 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