DECLARE @second INT
DECLARE @sensor_id INT
DECLARE @columncount INT
SET @sensor_id = 21
SELECT @second = MIN(abs(DATEDIFF(ss, '2007-01-25 17:00:01', [date]))) FROM T_DATA WHERE sensor_id = @sensor_id
SELECT @columncount =
COUNT(*) FROM T_DATA WHERE sensor_id = @sensor_id AND date = DATEADD(ss, @second, '2007-01-25 17:00:01')
IF @columncount = 0
SELECT * FROM T_DATA WHERE sensor_id = @sensor_id AND date = DATEADD(ss, -@second, '2007-01-25 17:00:01')
ELSE
SELECT * FROM T_DATA WHERE sensor_id = @sensor_id AND date = DATEADD(ss, @second, '2007-01-25 17:00:01')
DECLARE @second INT
DECLARE @sensor_id INT
DECLARE @columncount INT
SET @sensor_id = 6
SELECT @second = MIN(abs(DATEDIFF(ss, '2007-01-25 17:00:01', [date]))) FROM T_IDATA WHERE sensor_id = @sensor_id
SELECT @columncount =
COUNT(*) FROM T_IDATA WHERE sensor_id = @sensor_id AND date = DATEADD(ss, @second, '2007-01-25 17:00:01')
IF @columncount = 0
SELECT * FROM T_IDATA WHERE sensor_id = @sensor_id AND date = DATEADD(ss, -@second, '2007-01-25 17:00:01') ORDER BY depth
ELSE
SELECT * FROM T_IDATA WHERE sensor_id = @sensor_id AND date = DATEADD(ss, @second, '2007-01-25 17:00:01') ORDER BY depth