In a SQL Server 2005 database I have a table with data logged when a signal is enabled or disabled. Simplified it looks like this:
Signal State Time
------ -------- ----------------
Alarm1 Disabled 2011-11-15 09:00
Alarm1 Enabled 2011-12-20 13:30
Alarm2 Enabled 2011-11-17 15:01
Alarm2 Disabled 2011-12-15 06:57
Alarm3 Disabled 2011-11-10 11:42
Alarm3 Enabled 2011-12-02 13:12
Alarm3 Disabled 2011-12-24 14:41
Alarm4 Enabled 2011-10-09 13:25
Alarm4 Disabled 2012-01-07 08:29
Alarm5 Disabled 2011-11-19 07:12
Alarm5 Enabled 2011-11-28 15:48
Alarm6 Disabled 2011-12-14 17:29
Alarm6 Enabled 2011-12-23 23:46
(All rows also have an ID column with a uniqueidentifier.)
Using T-SQL I want to extract information on which alarms was disabled during a given time frame, e.g. December 2011. The result should not only consist of those alarms that were logged as disabled that month, but also include alarms that were disabled earlier and not enabled during the given time frame.
Given the data above I would like to extract something like:
Signal Disabled Enabled
------ ---------------- -------
Alarm1 - 2011-12-20 12:30
Alarm2 2011-12-15 06:57 -
Alarm3 - 2011-12-02 13:12
Alarm3 2011-12-24 14:41 -
Alarm6 2011-12-14 17:29 2011-12-23 23:46
Any help appreciated!
Simplified types.
I have two suggestions (depending on what data you want)
First some test data:
Then if you what the alarm not unique. I would do something like this:
If you what the alarms unique the I would do something like this: