My first post, so bear with me. I want to sum based upon a value that is broken by dates but only want the sum for the dates, not for the the group by item in total. Have been working on this for days, trying to avoid using a cursor but may have to.
Here's an example of the data I'm looking at. BTW, this is 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 look like 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 to get the Key to fill in the Nulls. The key isn't always entered in but is assumed to be the value until actually changed.
SELECT key ,time ,amt
FROM (
SELECT DISTINCT amt, time,
,last_value(amt ignore nulls) OVER (
ORDER BY time
) key
FROM sample
ORDER BY time, amt
)
WHERE amt > 0
ORDER BY time, key NULLS first;
But when I try to get just a running total, it sums on the key even with the breaks. I cannot figure out how to get it break on the key. Here's my best shot at it which isn't very good and doesn't work correctly.
SELECT key,time, amt
, sum(amt) OVER (PARTITION BY key ORDER BY time) AS running_total
FROM (SELECT key, time, amt
FROM (SELECT DISTINCT
amt,
time,
last_value(amt ignore nulls) OVER (ORDER BY time) key
FROM sample
ORDER BY time, amt
)
WHERE amt > 0
ORDER BY time, key NULLS first
)
ORDER BY time, key NULLS first;
Any help would be appreciated. Maybe using cursor is the only way.
Match sample data.
I'm not sure what your example data has to do with the queries (your sample data is one table, for instance and the sample queries have many queries). But, for assigning the keys, you can use
LAG()
with theIGNORE NULLS
option:Then, you want to group groups of the same key together. One method is a difference of row numbers. The final step is aggregation:
Assigning a group number whenever
Key
is not NULL can easily be calculated together with theLAG
:In order to get the sums you are looking for you need a way to group the values you are interested in. You can generate a grouping ID by using the a couple of
ROW_NUMBER
analytic functions, one partitioned by the key value. However due to your need to duplicate theKEY
column values this will need to be done in a couple of stages:The above query creates a running sum of AMT that restarts every time the key value changes. Whereas the following query used in place of the last select statement above gives the requested results, which I wouldn't term a running sum.
To see the full time values you may want to either alter your session
NLS_DATE_FORMAT
as below:Or wrap each date column in a
TO_CHAR
function for output purposes.