1

我有两张桌子

活动

EventCode    EventDate
1            2013-07-22 04:00:00.000
2            2013-07-21 11:00:00.000
3            2013-07-21 10:00:00.000

DeviceSignal是事务表


TimeStamp                   DeviceID    Value
2013-07-22 03:10:00.000     Dev1             33
2013-07-22 03:00:00.000     Dev2             22
2013-07-21 11:00:00.000     Dev3             23
2013-07-21 10:50:00.000     Dev4             45
2013-07-21 10:40:00.000     Dev5             23
2013-07-21 10:30:00.000     Dev1             54
2013-07-21 10:20:00.000     Dev2             22
2013-07-21 09:50:00.000     Dev1             11
2013-07-21 09:40:00.000     Dev2             23

基于 EventDate,我想获取时间戳、设备 ID 和值,TimeStamp 在 EventDate Hierarchy 中,我怎样才能得到低于输出?


EventCode   EventDate               TimeStamp               DeviceID    Value
1           2013-07-22 04:00:00.000 2013-07-22 03:10:00.000  Dev1       33
1           2013-07-22 04:00:00.000 2013-07-22 03:00:00.000  Dev2       22
2           2013-07-21 11:00:00.000 2013-07-21 10:50:00.000  Dev4       45
2           2013-07-21 11:00:00.000 2013-07-21 10:40:00.000  Dev5       23
2           2013-07-21 11:00:00.000 2013-07-21 10:30:00.000  Dev1       54
2           2013-07-21 11:00:00.000 2013-07-21 10:20:00.000  Dev2       22
3           2013-07-21 10:00:00.000 2013-07-21 09:50:00.000  Dev1       11
3           2013-07-21 10:00:00.000 2013-07-21 09:40:00.000  Dev2       23
4

3 回答 3

2
SELECT
    e.EventCode, e.EventDate,
    d.TimeStamp, d.DeviceID, d.Value
FROM 
    DeviceSignal AS d
  OUTER APPLY
    ( SELECT TOP (1) e.EventCode, e.EventDate
      FROM Events AS e
      WHERE d.TimeStamp <= e.EventDate
      ORDER BY e.EventDate
    ) AS e ;
于 2013-10-07T11:15:01.720 回答
0
SELECT EV.[EventCode],EV.[EventDate],DS.[TimeStamp],DS.DeviceID,DS.[Value] 
from [Events] EV INNER JOIN [DeviceSignal] DS
on CONVERT(DATE,ev.EventDate)=CONVERT(DATE,DS.[TimeStamp])
于 2013-10-07T11:22:06.247 回答
0

我想这就是你要找的:

SELECT 
    EV.EventCode, 
    EV.EventDate,
    DS.Timestamp,
    DS.DeviceID,
    DS.Value
FROM
    Events EV JOIN DeviceSignal DS 
        ON CAST(EV.EventDate AS DATE) = CAST(DS.TimeStamp AS DATE)
于 2013-10-07T11:14:58.563 回答