This is my input:
Converted Time PUMP
----------------------- --------
Do 30-Okt-2014 10:50:09 FALSE
Do 30-Okt-2014 11:23:39 TRUE
Do 30-Okt-2014 11:48:37 FALSE
Do 30-Okt-2014 12:53:34 TRUE
Do 30-Okt-2014 13:07:38 FALSE
Do 30-Okt-2014 14:36:58 TRUE
Do 30-Okt-2014 15:02:33 FALSE
Do 30-Okt-2014 16:03:59 TRUE
If PUMP = TRUE
means the pump is turned on. I have to look how many minutes it is turned on for each hour. So I look for the next PUMP = FALSE
and calculate the minutes till it is turned off and so on. This is finally my output stored in a DataSet
:
Hours Minutes_on
---------------------- ----------------
30.10.2014 11:00:00 25
30.10.2014 12:00:00 7
30.10.2014 13:00:00 7
30.10.2014 14:00:00 24
30.10.2014 15:00:00 2
And here is my question, what is the most efficient way to calculate this? I'm now doing it with these steps but it takes very long:
Step1: create a minute-dataset for the date range for every minute:
30.10.2014 11:23:00 empty
30.10.2014 11:24:00 empty
30.10.2014 11:25:00 empty
30.10.2014 11:26:00 empty
30.10.2014 11:27:00 empty
30.10.2014 11:28:00 empty
30.10.2014 11:29:00 empty
30.10.2014 11:30:00 empty
30.10.2014 11:31:00 empty
30.10.2014 11:32:00 empty
30.10.2014 11:33:00 empty
30.10.2014 11:34:00 empty
30.10.2014 11:35:00 empty
30.10.2014 11:36:00 empty
30.10.2014 11:37:00 empty
30.10.2014 11:38:00 empty
30.10.2014 11:39:00 empty
30.10.2014 11:40:00 empty
30.10.2014 11:41:00 empty
30.10.2014 11:42:00 empty
30.10.2014 11:43:00 empty
30.10.2014 11:44:00 empty
30.10.2014 11:45:00 empty
30.10.2014 11:46:00 empty
30.10.2014 11:47:00 empty
30.10.2014 11:48:00 empty
30.10.2014 11:49:00 empty
Step2: Loop through my input and set the state in my minute-dataset
30.10.2014 11:23:00 On '-> is in my input table
30.10.2014 11:24:00 empty
30.10.2014 11:25:00 empty
30.10.2014 11:26:00 empty
30.10.2014 11:27:00 empty
30.10.2014 11:28:00 empty
30.10.2014 11:29:00 empty
30.10.2014 11:30:00 empty
30.10.2014 11:31:00 empty
30.10.2014 11:32:00 empty
30.10.2014 11:33:00 empty
30.10.2014 11:34:00 empty
30.10.2014 11:35:00 empty
30.10.2014 11:36:00 empty
30.10.2014 11:37:00 empty
30.10.2014 11:38:00 empty
30.10.2014 11:39:00 empty
30.10.2014 11:40:00 empty
30.10.2014 11:41:00 empty
30.10.2014 11:42:00 empty
30.10.2014 11:43:00 empty
30.10.2014 11:44:00 empty
30.10.2014 11:45:00 empty
30.10.2014 11:46:00 empty
30.10.2014 11:47:00 empty
30.10.2014 11:48:00 Off '-> is in my input table
30.10.2014 11:49:00 empty
Step 3: Loop through my minute-dataset and set the cells between with the right values
30.10.2014 11:23:00 On
30.10.2014 11:24:00 On
30.10.2014 11:25:00 On
30.10.2014 11:26:00 On
30.10.2014 11:27:00 On
30.10.2014 11:28:00 On
30.10.2014 11:29:00 On
30.10.2014 11:30:00 On
30.10.2014 11:31:00 On
30.10.2014 11:32:00 On
30.10.2014 11:33:00 On
30.10.2014 11:34:00 On
30.10.2014 11:35:00 On
30.10.2014 11:36:00 On
30.10.2014 11:37:00 On
30.10.2014 11:38:00 On
30.10.2014 11:39:00 On
30.10.2014 11:40:00 On
30.10.2014 11:41:00 On
30.10.2014 11:42:00 On
30.10.2014 11:43:00 On
30.10.2014 11:44:00 On
30.10.2014 11:45:00 On
30.10.2014 11:46:00 On
30.10.2014 11:47:00 On
30.10.2014 11:48:00 Off
30.10.2014 11:49:00 Off
Step 4: create a hour-dataset for the given date range and sum the minutes where they On
Hours Minutes_on
---------------------- ----------------
30.10.2014 11:00:00 25
30.10.2014 12:00:00 7
30.10.2014 13:00:00 7
30.10.2014 14:00:00 24
30.10.2014 15:00:00 2
Maybe there is any date function I don't know to save some loops? Thanks!
Current database Query
command.CommandText = "WITH table_ AS
(SELECT DATETIME,
TOTALTIME1,
RAWOUTPUT1,
CASE BITAND(RAWOUTPUT1, POWER(2," + (DD_einzelnePumpe.SelectedIndex - 1) + ")) 'e.g. POWER(2,0)
WHEN 0 THEN 'FALSE'
ELSE 'TRUE'
END AS Pumpe1_1,
CASE BITAND(LAG(RAWOUTPUT1) OVER (ORDER BY datetime), POWER(2," + (DD_einzelnePumpe.SelectedIndex - 1) + "))
WHEN 0 THEN 'FALSE'
ELSE 'TRUE'
END AS priv_,
CASE BITAND(LEAD(RAWOUTPUT1) OVER (ORDER BY datetime), POWER(2," + (DD_einzelnePumpe.SelectedIndex - 1) + "))
WHEN 0 THEN 'FALSE'
ELSE 'TRUE'
END AS next_
FROM pump_box_hist
WHERE DATETIME > to_date('" + Start + "','dd/mm/yyyy hh24:mi:ss')
AND DATETIME < to_date('" + Ende + "','dd/mm/yyyy hh24:mi:ss')
)
SELECT DATETIME,
TOTALTIME1,
RAWOUTPUT1,
Pumpe1_1
FROM table_
WHERE ((Pumpe1_1 = next_ or next_ is null)
AND (Pumpe1_1 <> priv_ or priv_ is null)
)";