Wednesday 15 August 2012

sql server 2008 - Tieing Two "Event Tables" Together with Timestamps SQL QUery -



sql server 2008 - Tieing Two "Event Tables" Together with Timestamps SQL QUery -

i have 2 db tables in sql server. have query result tie operator name row record in table. i.e. each row record in "table b", check see operator logged in @ time of event. exception when no operator logged in , there null field in result.

the screenshot below outlines 2 tables , how sample result need look. help appreciated...i out of league here.

tablea operator name| log on time |log out time

tom 10/01/2014 12:00 10/01/2014 12:09

dick 10/01/2014 12:10 10/01/2014 12:35

harry 10/01/2014 12:40 10/01/2014 12:45

tableb timestamp| event

10/01/2014 12:03 a

10/01/2014 12:15 b

10/01/2014 12:36 a

required query results operator name|timestamp|event name

tom 10/01/2014 12:03 a

dick 10/01/2014 12:15 b

"no operator" 10/01/2014 12:36 a

try outer apply this:

select a.operatorname ,b.timestamp ,b.eventname tableb b outer apply ( select isnull(a.operatorname,'no operator') tablea b.timestamp between a.logontime , a.logouttime )

sql sql-server-2008

No comments:

Post a Comment