|
mdutra
Starting Member
2 Posts |
Posted - 2011-12-07 : 10:07:02
|
| Here is some sample data:SET NOCOUNT ON;GOUSE tempdb;GOCREATE TABLE [continuous_data]([case_id] [int] NOT NULL,[channel_index] [smallint] NOT NULL,[start_time] [datetime] NOT NULL,[dms_type] [char](1) NOT NULL,[dms_value] [float] NOT NULL,[value_duration] [smallint] NOT NULL);GOInsert into continuous_data(case_id, channel_index, start_time, dms_type, dms_value, value_duration)select 2674,0,'2011-09-30 15:32:46.000','0',70.11,1 union allselect 2674,1,'2011-09-30 15:32:46.000','0',15,1 union allselect 2674,2,'2011-09-30 15:32:46.000','0',96.1,1 union allselect 2674,3,'2011-09-30 15:32:46.000','0',3.5,1 union allselect 2674,0,'2011-09-30 15:32:47.000','0',70.2,1 union allselect 2674,1,'2011-09-30 15:32:47.000','0',15.2,1 union allselect 2674,2,'2011-09-30 15:32:47.000','0',96,1 union allselect 2674,3,'2011-09-30 15:32:47.000','0',3.5,1 union allselect 2674,0,'2011-09-30 15:32:48.000','0',65.4,1 union allselect 2674,1,'2011-09-30 15:32:48.000','0',15.2,1 union allselect 2674,2,'2011-09-30 15:32:48.000','0',95.8,1 union allselect 2674,3,'2011-09-30 15:32:48.000','0',3.5,1 union allselect 2674,0,'2011-09-30 15:32:49.000','0',60,1 union allselect 2674,1,'2011-09-30 15:32:49.000','0',15.1,1 union allselect 2674,2,'2011-09-30 15:32:49.000','0',94.5,1 union allselect 2674,3,'2011-09-30 15:32:49.000','0',3.5,1goThis is 4 seconds of data for 4 channels for a single case. In a real case we will have 2+ hours worth for 10+ channels and there will be thousands of cases in the database. I didn't want to make the sql I included here too huge but if more data is needed I can provide it. The time between each reading is not always 1 second either so that I cannot make any assumptions on the duration. The duration column is added by the device entering in the data to improve performance of subsequent data queries so it can be omitted if it provides no value.An example query is what I had in the first post but the repeat:Find all cases where channel 0 is below 65 for more than 3 consecutive minutes.Our brute force method uses cursors and simply loops through all the records case by case and adding up duration when dms_value is below 65 and adding the case if the sum reaches 180 and if the dms_value goes back over 65 clearing it out. Seems to me that there has to be a better way. |
 |
|