Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:23:58+00:00 2026-06-18T07:23:58+00:00

Hi I got Oracle Query which returning: SQLFIDDLEExample SensorKey StartTime EndTime 45 2012.10.17 08:31

  • 0

Hi I got Oracle Query which returning:

SQLFIDDLEExample

   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:

  1. Copy one row so many times how much Hours Overlap, example first row overlap 8 , 9 , 10 hours.

  2. Timekey = Date from StartTime in format YYYYMMDD

    Hour = Hour from StartTime in format H 24H

    Duration that 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:23:59+00:00Added an answer on June 18, 2026 at 7:23 am

    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 :))

    SQL> with data as (select rownum id, "SensorKey" s_key, cast("StartTime" as date) s_time, cast("EndTime" as date) e_time,
      2                       ((trunc(cast("EndTime" as date) , 'hh24') -trunc(cast("StartTime" as date), 'hh24')) *24)+1 hours
      3                  from table1)
      4  select to_char(block_start, 'yyyymmdd') "TimeKey",
      5         thehour "Hour", s_key "SensorKey",
      6         sum(duration) "Duration", min(s_time) "StartTime", max(e_time) "EndTime"
      7    from (select block_start, thehour, s_key, duration, s_time, e_time
      8            from data
      9            model partition by (id as key)
     10                  dimension by (0 as f)
     11                  measures (s_key, s_time, e_time,
     12                            cast(2 as number(2,0)) duration, hours,
     13                            cast(null as number(2)) thehour,
     14                            cast(null as date) block_start,
     15                            cast(null as date) block_end)
     16                  rules (block_start[for f from 0 to hours[0]-1 increment 1] = trunc(s_time[0] + (cv(f)/24), 'hh24'),
     17                         block_end[any] = trunc(s_time[0] + ((cv(f)+1)/24), 'hh24'),
     18                         s_key[any] = s_key[0],
     19                         s_time[any] = s_time[0],
     20                         e_time[any] = e_time[0],
     21                         duration [any] = case
     22                                            when cv(f) = 0
     23                                            then (least(block_end[cv(f)],e_time[0]) - s_time[0]) * 24*60
     24                                            when cv(f) =  hours[0]-1
     25                                            then (e_time[0] - block_start[cv(f)] ) * 24*60
     26                                            else (block_end[cv(f)] - block_start[cv(f)] ) * 24*60
     27                                          end,
     28                         thehour[any] = to_char(s_time[0] + (cv(f)/24), 'hh24')
     29                        ))
     30   group by block_start, thehour, s_key
     31   order by 5, 2;
    
    TimeKey        Hour  SensorKey   Duration StartTime         EndTime
    -------- ---------- ---------- ---------- ----------------- -----------------
    20121017          8         45         29 17-oct-2012 08:31 17-oct-2012 10:21
    20121017          9         45         60 17-oct-2012 08:31 17-oct-2012 10:21
    20121017         10         45         46 17-oct-2012 08:31 17-oct-2012 10:51
    20121017         12         45         24 17-oct-2012 12:21 17-oct-2012 13:41
    20121017         13         45         50 17-oct-2012 12:41 17-oct-2012 14:46
    20121017         14         45         46 17-oct-2012 13:51 17-oct-2012 14:46
    20121017         15         45         19 17-oct-2012 15:11 17-oct-2012 16:21
    20121017         16         45         21 17-oct-2012 15:46 17-oct-2012 16:21
    20121017         18         45          5 17-oct-2012 18:51 17-oct-2012 18:56
    20121017         19         45         45 17-oct-2012 19:11 17-oct-2012 19:56
    20121017         20         45         34 17-oct-2012 20:26 17-oct-2012 21:11
    20121017         21         45         11 17-oct-2012 20:26 17-oct-2012 21:11
    20121017         22         45         35 17-oct-2012 22:16 17-oct-2012 22:56
    20121018          0         45         60 17-oct-2012 23:36 18-oct-2012 01:46
    20121018          1         45         46 17-oct-2012 23:36 18-oct-2012 01:46
    20121017         23         45         24 17-oct-2012 23:36 18-oct-2012 01:46
    20121018          2         45         40 18-oct-2012 02:16 18-oct-2012 02:56
    20121018          3         45         29 18-oct-2012 03:31 18-oct-2012 15:06
    20121018          4         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018          5         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018          6         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018          7         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018          8         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018          9         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018         10         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018         11         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018         12         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018         13         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018         14         45         60 18-oct-2012 03:31 18-oct-2012 15:06
    20121018         15         45         35 18-oct-2012 03:31 18-oct-2012 16:41
    20121018         16         45         41 18-oct-2012 15:31 18-oct-2012 16:41
    20121018         17         45         19 18-oct-2012 17:41 18-oct-2012 18:06
    20121018         18         45          6 18-oct-2012 17:41 18-oct-2012 18:06
    20121018         19         45         15 18-oct-2012 19:16 18-oct-2012 19:41
    20121018         20         45          9 18-oct-2012 20:51 18-oct-2012 23:16
    20121018         21         45         60 18-oct-2012 20:51 18-oct-2012 23:16
    20121018         22         45         60 18-oct-2012 20:51 18-oct-2012 23:16
    20121018         23         45         16 18-oct-2012 20:51 18-oct-2012 23:16
    20121019          0         45         50 19-oct-2012 00:01 19-oct-2012 00:51
    
    39 rows selected.
    

    some notes:
    firstly i calculated the hours required per row.

    SQL> select rownum id, "SensorKey" s_key, cast("StartTime" as date) s_time, cast("EndTime" as date) e_time,
      2         ((trunc(cast("EndTime" as date) , 'hh24') -trunc(cast("StartTime" as date), 'hh24')) *24)+1 hours
      3    from table1;
    
            ID      S_KEY S_TIME            E_TIME                 HOURS
    ---------- ---------- ----------------- ----------------- ----------
             1         45 17-oct-2012 08:31 17-oct-2012 10:21          3
    

    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

    model partition by (id as key)
    

    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).

    measures (s_key, s_time, e_time, 
                              cast(null as number(2,0)) duration, hours, 
                              cast(null as number(2)) thehour,
                              cast(null as date) block_start,
                              cast(null as date) block_end)
    

    the cast() columns are just columns that aren’t in the original set but ones we’ll be computing as we go.
    duration will hold the minutes, thehour will show the hour number and the blocks will hold the hour slot that the current hour fits into.

    the rules is where all our logic is done..so:

    rules (block_start[for f from 0 to hours[0]-1 increment 1] = trunc(s_time[0] + (cv(f)/24), 'hh24'),
    

    "for f from 0 to hours[0]-1 increment 1" means we are generating rows based on the HOURS column (3 for the first row).

    block start will be set to 17-oct-2012 08:00 on the first row and block end 09:00 (on row 2 we bump them up an hour and so on.

    s_key[any] = s_key[0],
    s_time[any] = s_time[0],
    e_time[any] = e_time[0],
    

    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

     case 
      when cv(f) = 0
      then (least(block_end[cv(f)],e_time[0]) - s_time[0]) * 24*60
      when cv(f) =  hours[0]-1
      then (e_time[0] - block_start[cv(f)] ) * 24*60
      else (block_end[cv(f)] - block_start[cv(f)] ) * 24*60
    end
    

    i.e. cv(f) = 0 means the first output row (cv bieng a function to access the “current value” of, in this case, the F variable.
    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 minutes
    for all rows in between we just deduct the block end from the block start (ie 60 minutes)

    this would give us the output as:

    TimeKey        Hour  SensorKey   Duration StartTime         EndTime
    -------- ---------- ---------- ---------- ----------------- -----------------
    20121017          8         45         29 17-oct-2012 08:31 17-oct-2012 10:21
    20121017          9         45         60 17-oct-2012 08:31 17-oct-2012 10:21
    20121017         10         45         21 17-oct-2012 08:31 17-oct-2012 10:21
    20121017         10         45         25 17-oct-2012 10:26 17-oct-2012 10:51
    ...etc..
    

    but you said group the “10” rows, so now its a simple group by to finish this off:

    select to_char(block_start, 'yyyymmdd') "TimeKey",
           thehour "Hour", s_key "SensorKey", 
           sum(duration) "Duration", min(s_time) "StartTime", max(e_time) "EndTime"
      from (..our model query...)
      group by block_start, thehour, s_key;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an oracle query like this: SELECT floatColum0,floatColum1,ROUND(floatColum2,2) from table; which is returning
I got an Oracle database statement which displays the data of the last 7
I got a little problem i need a sql query that gives all rows
Calling all Oracle heads, I've got a weird problem with a sql query... Running
I got an oracle SQL query that selects entries of the current day like
I've got a barcode report which is using a sequence ( Oracle backend) to
I got a bit of a problem with a Oracle query create or replace
I've got a query which returns 30 rows. I'm writing code that will paginate
I need to query xml data using XQJ in my java application. I wanted
I have a query which works very fast on Oracle. field1 and field2 and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.