Question

We store the experimental data of the system in every one second. we also store Date-Time , system_ON_Status along with other parameters. The system is switched ON as per the requirement in a day (May be 3-4 times).

Now we need to generate the query to display the start and stop time of the experiment.

The system_ON_Status Field is stored as a boolean in SQL.

True for ON and False for OFF.

Thanks

We are using SQL SERVER 2008 R2

Date_Time                 System_ON_Status
2014-03-04 16:01:01:000     0
2014-03-04 16:01:02:000     0
2014-03-04 16:01:03:000     0
2014-03-04 16:01:04:000     0
2014-03-04 16:01:05:000     1
2014-03-04 16:01:06:000     1
2014-03-04 16:01:07:000     1
2014-03-04 16:01:08:000     1
2014-03-04 16:01:09:000     1
2014-03-04 16:01:10:000     0
2014-03-04 16:01:11:000     0
2014-03-04 16:01:12:000     1
2014-03-04 16:01:13:000     1
2014-03-04 16:01:14:000     1
2014-03-04 16:01:15:000     1
2014-03-04 16:01:16:000     0
2014-03-04 16:01:17:000     0
2014-03-04 16:01:18:000     0
2014-03-04 16:01:19:000     1
2014-03-04 16:01:20:000     1
2014-03-04 16:01:21:000     1
2014-03-04 16:01:22:000     1
2014-03-04 16:01:23:000     1
2014-03-04 16:01:24:000     1

The output must be: 1) Start of the experiment i.e 16:01:05:000 and End of experiment 16:01:09:000 System was operated for : Difference between 16:01:05:000 - 16:01:09:000 4sec

2) Start of the experiment i.e 16:01:12:000 and End of experiment 16:01:15:000 System was operated for : Difference between 16:01:05:000 - 16:01:09:000 3sec

2) Start of the experiment i.e 16:01:19:000 and End of experiment 16:01:24:000 System was operated for : Difference between 16:01:05:000 - 16:01:09:000 5sec

No correct solution

OTHER TIPS

The following query gives you the start and end times of each experiment. It includes the time where the system_on_status changes back to zero, as this is confirmation the experiment has ended. In the case of the last experiment it has not yet ended (there's no zero for system_on_status) so the end time is NULL.

SELECT  FromTime = MIN(D.log_date)
       ,X.ToTime
       ,D.system_on_status
FROM    Experiment_Log D
        OUTER APPLY ( SELECT TOP 1
                                ToTime = D2.log_date
                      FROM      Experiment_Log D2
                      WHERE     D.log_date < D2.log_date
                                AND D.[system_on_status] <> D2.[system_on_status]
                      ORDER BY  D2.log_date
                    ) X
WHERE   D.system_on_status = 1
GROUP BY X.ToTime
       ,D.system_on_status
ORDER BY FromTime;

Output from this for your data:

FromTime             ToTime               system_on_status
2014-03-04 16:01:05  2014-03-04 16:01:10  1
2014-03-04 16:01:12  2014-03-04 16:01:16  1
2014-03-04 16:01:19  NULL                 1

I can't really claim any glory for this, I took the query from this excellent answer and modified it for your table. So if you want to up-vote go and give ErikE the rep! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top