I’m pretty new to database programming, and I’m in need some help writing what seems to be a complicated t-sql query.
Our database stores time data along with various levels recorded at that time. A typical measurement will last for weeks or months. I need to write a query in SqlServer2005 to identify gaps in the time data to know when the instrument wasn’t communicating. Our sample interval can be either 1 sec or .5 sec, but it will always be the same in a given measurement (ie: all samples in a measurement will be either 1 or .5 samples/sec).
Ideally I would like to get a list of
[block1-start block1-end]
[block2-start block2-end]
etc
where each block is a unit of consecutive times within the overall measurement.
Are there any commands in TSQL that would make this query easier?
Check this out. Since you don’t have overlapping intervals you can write a simple SQL query to return these results. The SQL below creates a dummy table variable called @Events to simulate your measurements table. The final query outputs the gaps greater than 1 second (configurable via variable @MaxIntervalAllowedBetweenEvents).