我試圖做出基於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