Hi I got Oracle Query which returning:
SensorKey StartTime EndTime
45 2012.10.17 08:31 2012.10.17 10:21
45 2012.10.17 10:26 2012.10.17 10:51
45 2012.10.17 12:21 2012.10.17 12:26
45 2012.10.17 12:41 2012.10.17 13:41
45 2012.10.17 13:51 2012.10.17 14:46
45 2012.10.17 15:11 2012.10.17 15:16
45 2012.10.17 15:46 2012.10.17 16:21
45 2012.10.17 18:51 2012.10.17 18:56
45 2012.10.17 19:11 2012.10.17 19:56
45 2012.10.17 20:26 2012.10.17 21:11
45 2012.10.17 22:16 2012.10.17 22:21
45 2012.10.17 22:26 2012.10.17 22:56
45 2012.10.17 23:36 2012.10.18 01:46
45 2012.10.18 02:16 2012.10.18 02:56
45 2012.10.18 03:31 2012.10.18 15:06
45 2012.10.18 15:31 2012.10.18 16:41
45 2012.10.18 17:41 2012.10.18 18:06
45 2012.10.18 19:16 2012.10.18 19:26
45 2012.10.18 19:36 2012.10.18 19:41
45 2012.10.18 20:51 2012.10.18 23:16
45 2012.10.19 00:01 2012.10.19 00:51
I need to get result with all data. Example for first row:
SensorKey StartTime EndTime
45 2012.10.17 08:31 2012.10.17 10:21
like this and so on with other rows:
TimeKey Hour SensorKey Duration StartTime EndTime
20121017 8 45 29 2012.10.17 08:31 2012.10.17 10:21
20121017 9 45 60 2012.10.17 08:31 2012.10.17 10:21
20121017 10 45 21 2012.10.17 08:31 2012.10.17 10:21
Rules:
-
Copy one row so many times how much Hours Overlap, example first row overlap 8 , 9 , 10 hours.
-
Timekey= Date fromStartTimein formatYYYYMMDDHour= Hour fromStartTimein formatH24HDurationthat Hour duration in minutes.
If there are several rows in one hour they must be grouped example for two first rows:
I
TimeKey Hour SensorKey Duration StartTime EndTime
20121017 8 45 29 2012.10.17 08:31 2012.10.17 10:21
20121017 9 45 60 2012.10.17 08:31 2012.10.17 10:21
20121017 10 45 46 2012.10.17 08:31 2012.10.17 10:21
For Oracle, one approach is the Model clause (as we’re making up rows, the model clause can do this for us).
a few things. you seem to want only data down to the minute, so it would make sense to use date vs timestamp for this. secondly i’d avoid using case sensitive names in the table in oracle (in the below query I aliased your data back to non sensitive to make it a bit easier to code up :))
some notes:
firstly i calculated the hours required per row.
this “HOURS” column will drive the model clause to tell it how many rows to generate per source row. the rownum ID is just there for a unique key (as the other data didnt seem to guarantee uniqueness).
I partitioned on ID
which means that we’re taking each row as a seperate bit of processing.
in the measures we list the fields we are going to be working with (computing or just outputting).
the
cast()columns are just columns that aren’t in the original set but ones we’ll be computing as we go.durationwill hold the minutes,thehourwill show the hour number and theblockswill hold the hour slot that the current hour fits into.the rules is where all our logic is done..so:
"for f from 0 to hours[0]-1 increment 1"means we are generating rows based on theHOURScolumn (3 for the first row).block start will be set to
17-oct-2012 08:00on the first row and block end09:00(on row 2 we bump them up an hour and so on.the above three items are simply copied to the output set as-is. the ANY keyword means match all rows (we could put the “for f..” logic here, but ANY is neater.
duration is calculated with the case statement
i.e.
cv(f) = 0means the first output row (cvbieng a function to access the “current value” of, in this case, theFvariable.so the first row, we take block end (
17-oct-2012 09:00) – the start time(17-oct-2012 08:31) and get that in minutes (29 minutes).for the end row again we take end time (
17-oct-2012 10:21) – the block start (17-oct-2012 10:00) = 21 minutesfor all rows in between we just deduct the block end from the block start (ie 60 minutes)
this would give us the output as:
but you said group the “10” rows, so now its a simple group by to finish this off: