Running total over repeating group by items based on time in Oracle SQL -


my first post, bear me. want sum based upon value broken dates want sum dates, not the group item in total. have been working on days, trying avoid using cursor may have to.

here's example of data i'm looking at. btw, in oracle 11g.

 key     time               amt ------ ------------------ ------  null    1-1-2016  00:00    50  null    1-1-2016  02:00    50  key1    1-1-2016  04:00    30  null    1-1-2016  06:00    30  null    1-1-2016  08:00    30  key2    1-1-2016  10:00    40  null    1-1-2016  12:00    40  key1    1-1-2016  14:00    30  null    1-2-2016  00:00    30  key2    1-2-2016  02:00    35 

the final result should this:

 key    start            stop             amt ------ ---------------- ---------------- -----  null   1-1-2016 00:00   1-1-2016 02:00   100  key1   1-1-2016 04:00   1-1-2016 08:00    90  key2   1-1-2016 10:00   1-1-2016 12:00    80  key1   1-1-2016 14:00   1-2-2016 00:00    60  key2   1-2-2016 02:00   1-2-2016 02:00    35 

i've been able key fill in nulls. key isn't entered in assumed value until changed.

select key ,time ,amt (     select distinct amt, time,          ,last_value(amt ignore nulls) on (             order time             ) key     sample     order time, amt     ) amt > 0 order time, key nulls first; 

but when try running total, sums on key breaks. cannot figure out how break on key. here's best shot @ isn't , doesn't work correctly.

select key,time, amt       , sum(amt) on (partition key order time) running_total   (select key, time, amt           (select distinct                          amt,                          time,                           last_value(amt ignore nulls) on (order time) key                   sample                  order time, amt                )          amt > 0          order time, key nulls first        ) order time, key nulls first; 

any appreciated. maybe using cursor way.

match sample data.

in order sums looking need way group values interested in. can generate grouping id using couple of row_number analytic functions, 1 partitioned key value. due need duplicate key column values need done in couple of stages:

with t1 (   select dta.*        , last_value(key ignore nulls)          -- fill in missing                on (order time asc) key2   -- key values     your_data dta ), t2 (   select t1.*        , row_number() on (order time)     -- generate        - row_number() on (partition key2  -- grouping id                                 order time) gp     t1 ) select t2.*      , sum(amt) on (partition gp, key2                           order time) running_sums   t2; 

the above query creates running sum of amt restarts every time key value changes. whereas following query used in place of last select statement above gives requested results, wouldn't term running sum.

select key2      , min(time) start_time      , max(time) stop_time      , sum(amt) amt   t2  group key2, gp; 

to see full time values may want either alter session nls_date_format below:

alter session set nls_date_format='dd-mm-rrrr hh24:mi:ss'; 

or wrap each date column in to_char function output purposes.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -