我有两张桌子
活动
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,我想得到timestamp,deviceID和Value,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
答案 0 :(得分: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 ;
答案 1 :(得分: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)
答案 2 :(得分: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])