tsql - SQL how can i select data between dates? -
i have 2 tables
events
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 transaction table
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
base on eventdate , timestamp , deviceid , value, timestamp in eventdate hierarchy , how can below output ?
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
select e.eventcode, e.eventdate, d.timestamp, d.deviceid, d.value devicesignal d outer apply ( select top (1) e.eventcode, e.eventdate events e d.timestamp <= e.eventdate order e.eventdate ) e ;
Comments
Post a Comment