Rolling Average in Oracle SQL - oracle

LotNumber Device Measure MeasureDate RowNumber
LotA DevA 1.1 10/1/15 0:00 1
LotA DevA 1.2 10/1/15 1:00 1
LotA DevB 1.1 10/1/15 2:00 2
LotB DevA 1.3 10/1/15 3:00 3
LotB DevA 1.4 10/1/15 4:00 3
LotA DevA 1.2 10/1/15 5:00 4
LotC DevA 1.3 10/1/15 6:00 5
LotD DevA 1.5 10/1/15 7:00 6
LotE DevA 1.1 10/1/15 8:00 7
LotF DevA 1.2 10/1/15 9:00 8
LotG DevA 1.3 10/1/15 10:00 9
LotH DevA 1.4 10/1/15 11:00 10
LotNumber Device Measure MeasureDate RowNumber Rolling Average
LotA DevA 1.1 10/1/15 0:00 1 Measure Average of RowNumber 1-5
LotA DevB 1.1 10/1/15 2:00 2 Measure Average of RowNumber 2-6
LotB DevA 1.3 10/1/15 3:00 3 Measure Average of RowNumber 3-7
LotA DevA 1.2 10/1/15 5:00 4 Measure Average of RowNumber 4-8
LotC DevA 1.3 10/1/15 6:00 5 Measure Average of RowNumber 5-9
LotD DevA 1.5 10/1/15 7:00 6 Measure Average of RowNumber 6-10
LotE DevA 1.1 10/1/15 8:00 7 Measure Average of RowNumber 7-10
LotF DevA 1.2 10/1/15 9:00 8 Measure Average of RowNumber 8-10
LotG DevA 1.3 10/1/15 10:00 9 Measure Average of RowNumber 9-10
LotH DevA 1.4 10/1/15 11:00 10 Measure Average of RowNumber 10
Is it possible to have the second table? I have no idea how to do this. Rolling average of rownumber with intervals of 4. For example, Average of RowNumber 1-5 is the average of all Measure with Rownumber that is ranging from 1-5. Thanks!

Yes, you can easily achieve this through the use of the avg analytic function, something like:
with sample_data (LotNumber, Device, Measure, MeasureDate, RowNumber) as
(select 'LotA', 'DevA', 1.1, to_date('10/1/15 00:00', 'dd/mm/yyyy hh24:mi'), 1
from dual union all
select 'LotA', 'DevA', 1.2, to_date('10/1/15 01:00', 'dd/mm/yyyy hh24:mi'), 1
from dual union all
select 'LotA', 'DevB', 1.1, to_date('10/1/15 02:00', 'dd/mm/yyyy hh24:mi'), 2
from dual union all
select 'LotB', 'DevA', 1.3, to_date('10/1/15 03:00', 'dd/mm/yyyy hh24:mi'), 3
from dual union all
select 'LotB', 'DevA', 1.4, to_date('10/1/15 04:00', 'dd/mm/yyyy hh24:mi'), 3
from dual union all
select 'LotA', 'DevA', 1.2, to_date('10/1/15 05:00', 'dd/mm/yyyy hh24:mi'), 4
from dual union all
select 'LotC', 'DevA', 1.3, to_date('10/1/15 06:00', 'dd/mm/yyyy hh24:mi'), 5
from dual union all
select 'LotD', 'DevA', 1.5, to_date('10/1/15 07:00', 'dd/mm/yyyy hh24:mi'), 6
from dual union all
select 'LotE', 'DevA', 1.1, to_date('10/1/15 08:00', 'dd/mm/yyyy hh24:mi'), 7
from dual union all
select 'LotF', 'DevA', 1.2, to_date('10/1/15 09:00', 'dd/mm/yyyy hh24:mi'), 8
from dual union all
select 'LotG', 'DevA', 1.3, to_date('10/1/15 10:00', 'dd/mm/yyyy hh24:mi'), 9
from dual union all
select 'LotH', 'DevA', 1.4, to_date('10/1/15 11:00', 'dd/mm/yyyy hh24:mi'), 10
from dual)
select lotnumber,
device,
measure,
measuredate,
rownumber,
avg(measure) over (order by rownumber
rows between current row and 4 following) rolling_average
from sample_data
order by rownumber;
LOTNUMBER DEVICE MEASURE MEASUREDATE ROWNUMBER ROLLING_AVERAGE
--------- ------ ---------- ------------------ ---------- ---------------
LotA DevA 1.1 10/01/0015 00:00 1 1.22
LotA DevA 1.2 10/01/0015 01:00 1 1.24
LotA DevB 1.1 10/01/0015 02:00 2 1.26
LotB DevA 1.3 10/01/0015 03:00 3 1.34
LotB DevA 1.4 10/01/0015 04:00 3 1.3
LotA DevA 1.2 10/01/0015 05:00 4 1.26
LotC DevA 1.3 10/01/0015 06:00 5 1.28
LotD DevA 1.5 10/01/0015 07:00 6 1.3
LotE DevA 1.1 10/01/0015 08:00 7 1.25
LotF DevA 1.2 10/01/0015 09:00 8 1.3
LotG DevA 1.3 10/01/0015 10:00 9 1.35
LotH DevA 1.4 10/01/0015 11:00 10 1.4
N.B. you didn't mention any grouping (eg. per day, per lotnumber, etc) and I used the rownumber column for the ordering - maybe it should have been the measuredate column?). If your requirements are more complex than what you've stated, you'll need to amend the over () clause appropriately.
With your additional comment that clarifies your requirements, you can amend the windowing clause to be on range, rather than rows between, like so:
with sample_data (LotNumber, Device, Measure, MeasureDate, RowNumber) as
(select 'LotA', 'DevA', 1.1, to_date('10/1/15 00:00', 'dd/mm/yyyy hh24:mi'), 1
from dual union all
select 'LotA', 'DevA', 1.2, to_date('10/1/15 01:00', 'dd/mm/yyyy hh24:mi'), 1
from dual union all
select 'LotA', 'DevB', 1.1, to_date('10/1/15 02:00', 'dd/mm/yyyy hh24:mi'), 2
from dual union all
select 'LotB', 'DevA', 1.3, to_date('10/1/15 03:00', 'dd/mm/yyyy hh24:mi'), 3
from dual union all
select 'LotB', 'DevA', 1.4, to_date('10/1/15 04:00', 'dd/mm/yyyy hh24:mi'), 3
from dual union all
select 'LotA', 'DevA', 1.2, to_date('10/1/15 05:00', 'dd/mm/yyyy hh24:mi'), 4
from dual union all
select 'LotC', 'DevA', 1.3, to_date('10/1/15 06:00', 'dd/mm/yyyy hh24:mi'), 5
from dual union all
select 'LotD', 'DevA', 1.5, to_date('10/1/15 07:00', 'dd/mm/yyyy hh24:mi'), 6
from dual union all
select 'LotE', 'DevA', 1.1, to_date('10/1/15 08:00', 'dd/mm/yyyy hh24:mi'), 7
from dual union all
select 'LotF', 'DevA', 1.2, to_date('10/1/15 09:00', 'dd/mm/yyyy hh24:mi'), 8
from dual union all
select 'LotG', 'DevA', 1.3, to_date('10/1/15 10:00', 'dd/mm/yyyy hh24:mi'), 9
from dual union all
select 'LotH', 'DevA', 1.4, to_date('10/1/15 11:00', 'dd/mm/yyyy hh24:mi'), 10
from dual)
select lotnumber,
device,
measure,
measuredate,
rownumber,
avg(measure) over (order by rownumber
range between current row and 4 following) rolling_average,
row_number() over (partition by rownumber order by measuredate) rn
from sample_data
order by rownumber;
LOTNUMBER DEVICE MEASURE MEASUREDATE ROWNUMBER ROLLING_AVERAGE RN
--------- ------ ---------- --------------------- ---------- --------------- ----------
LotA DevA 1.1 10/01/0015 00:00:00 1 1.22857143 1
LotA DevA 1.2 10/01/0015 01:00:00 1 1.22857143 2
LotA DevB 1.1 10/01/0015 02:00:00 2 1.3 1
LotB DevA 1.3 10/01/0015 03:00:00 3 1.3 1
LotB DevA 1.4 10/01/0015 04:00:00 3 1.3 2
LotA DevA 1.2 10/01/0015 05:00:00 4 1.26 1
LotC DevA 1.3 10/01/0015 06:00:00 5 1.28 1
LotD DevA 1.5 10/01/0015 07:00:00 6 1.3 1
LotE DevA 1.1 10/01/0015 08:00:00 7 1.25 1
LotF DevA 1.2 10/01/0015 09:00:00 8 1.3 1
LotG DevA 1.3 10/01/0015 10:00:00 9 1.35 1
LotH DevA 1.4 10/01/0015 11:00:00 10 1.4 1
Note that I've included the "rn" column, because I wasn't sure if you wanted to filter out the "duplicate" rownumber rows or not - if you do, then you'll need to add an outer query that filters on rn = 1.
N.B. It would have been helpful if you had included the actual output values you were expecting to see in the rolling average column rather than just the logic, so that we could compare our results with it.

Related

Oracle rounding down sysdate to the nearest minute point that divisible by 30

I have to convert sysdate by rounding down to nearest minute point that divisible by 30. For example:
If sysdate is between 2020-10-14 09:00:00 and 2020-10-14 09:29:59 then return 2020-10-14 09:00:00
If sysdate is between 2020-10-14 09:30:00 and 2020-10-14 09:59:59 then return 2020-10-14 09:30:00
How can I get my expected result in Oracle?
The minutes logic here
get the minutes
divide by 30 and truncate (which gives 0 or 1)
*30/1440 to get 0 or 30minutes of a day
and then add to the hour of day
SQL> with d as
2 ( select to_date('09:27','HH:MI') x from dual
3 union all
4 select to_date('09:37','HH:MI') x from dual
5 )
6 select x, trunc(x,'HH') + 30*trunc(to_number(to_char(x,'MI'))/30)/1440
7 from d;
X TRUNC(X,'HH')+30*TR
------------------- -------------------
01/10/2020 09:27:00 01/10/2020 09:00:00
01/10/2020 09:37:00 01/10/2020 09:30:00

Split 2 db rows into 3 by date ranges

I have a problem I can't solve. I have A and B money which I can spend in a defined period. These are the following two rows in the DB (with begin_date, end_date and amount columns):
A: 2015.01.01.-2015.09.30. 10.000$
B: 2015.07.01.-2015.12.31. 7.000$
So these dates are overlapped, and it means I can spend more money between 2017.07.01. and 2015.09.30. So in the output I have to get the following:
2015.01.01.-2015.07.01. x$
2015.07.01.-2015.09.30. y$
2015.09.30.-2015.12.31. z$
How can I select these ranges and count the amounts considering I spend money equally per months? If I can define the 3 date ranges I think I can count the amounts, but the dates are really tricky, and I can't handle them.
I use Oracle 11g.
Borrowing heavily from this approach, which is also explained here in more detail along with some alternatives, to just get the date ranges you can do:
with cte1 as
(
select begin_date as marker_date, 1 as type
from your_table
union all
select end_date + 1 as marker_date, -1 as type
from your_table
),
cte2 as (
select marker_date as begin_date,
lead(marker_date) over (order by marker_date) - 1 as end_date,
sum(type) over (order by marker_date) as periods
from cte1
)
select begin_date, end_date from cte2
where end_date is not null and periods > 0;
Which gives you:
BEGIN_DATE END_DATE
---------- ----------
2015-01-01 2015-06-30
2015-07-01 2015-09-30
2015-10-01 2015-12-31
I've assumed that you don't actually want the generated periods to overlap by a day, and instead want them to be the start and ends of months like the original two rows.
To get the amounts - if I've understood what you described - you can modify that to include the amount change at each date, as either positive or negative depending on whether it's the start or end of a period:
with cte1 as
(
select begin_date as marker_date,
amount / months_between(end_date + 1, begin_date) as monthly_amount
from your_table
union all
select end_date + 1 as marker_date,
-amount / months_between(end_date + 1, begin_date) as monthly_amount
from your_table
),
cte2 as (
select marker_date as begin_date,
lead(marker_date) over (order by marker_date) - 1 as end_date,
sum(monthly_amount) over (order by marker_date) as total_monthly_amount
from cte1
)
select begin_date, end_date,
total_monthly_amount * months_between(end_date + 1, begin_date) as amount
from cte2
where end_date is not null and total_monthly_amount > 0;
BEGIN_DATE END_DATE AMOUNT
---------- ---------- ----------
2015-01-01 2015-06-30 6.66666667
2015-07-01 2015-09-30 6.83333333
2015-10-01 2015-12-31 3.5
This works by dividing the amount for the original period by the number of months it covers:
select begin_date as marker_date, amount,
months_between(end_date + 1, begin_date) as months,
amount / months_between(end_date + 1, begin_date) as monthly_amount
from your_table
union all
select end_date + 1 as marker_date, amount,
months_between(end_date + 1, begin_date) as months,
-amount / months_between(end_date + 1, begin_date) as monthly_amount
from your_table;
MARKER_DATE AMOUNT MONTHS MONTHLY_AMOUNT
----------- ---------- ---------- --------------
2015-01-01 10 9 1.11111111
2015-07-01 7 6 1.16666667
2015-10-01 10 9 -1.11111111
2016-01-01 7 6 -1.16666667
And then using that as a CTE and applying the lead analytic function to reconstruct the new, non-overlapping periods:
with cte1 as
(
select begin_date as marker_date,
months_between(end_date + 1, begin_date) as months,
amount / months_between(end_date + 1, begin_date) as monthly_amount
from your_table
union all
select end_date + 1 as marker_date,
months_between(end_date + 1, begin_date) as months,
-amount / months_between(end_date + 1, begin_date) as monthly_amount
from your_table
)
select marker_date as begin_date,
lead(marker_date) over (order by marker_date) - 1 as end_date,
sum(monthly_amount) over (order by marker_date) as total_monthly_amount,
months_between(lead(marker_date) over (order by marker_date), marker_date) as months
from cte1;
BEGIN_DATE END_DATE TOTAL_MONTHLY_AMOUNT MONTHS
---------- ---------- -------------------- ----------
2015-01-01 2015-06-30 1.11111111 6
2015-07-01 2015-09-30 2.27777778 3
2015-10-01 2015-12-31 1.16666667 3
2016-01-01 0.00000000
And finally excluding the artificial open-ended period at the end, plus any that have a zero total in case there are any gaps (which you don't have in the small sample, but could appear in a larger data set); and multiplying the new monthly amount by the number of months in the new period.

Select Top (Max) Amount From Two Of Four Fields (Columns)

I have this query
SELECT code, username, week1money, week2money, week3money, week4money FROM(
--subquery goes here
)
How to select the top two weeks, i.e. weeks with the highest value? I want to sum the top two weeks to be precise.
If I understand correct you want to get 2 top values per every (code, username) row and (code, username) is a key of recordset.
Supposing you can have two top weeks with the same values and you don't have nulls this might be one of solutions:
SQL> with t (id, code, week1, week2, week3, week4)
2 as (
3 select 1, 'a', 10, 15, 11, 8 from dual union all
4 select 2, 'b', 7, 4, 2, 9 from dual union all
5 select 3, 'c', 3, 3, 1, 0 from dual
6 )
7 select id, code, max(week) first_top, min(week) next_top from (
8 select id, code, row_number() over(partition by id, code order by week desc) rnk, week
9 from (
10 select t.id, t.code,
11 decode(r.rn,1,week1,2,week2,3,week3,4,week4) week
12 from t,
13 (select rownum rn from dual connect by level <= 4) r
14 ))
15 where rnk in (1,2)
16 group by id, code
17 /
ID C FIRST_TOP NEXT_TOP
---------- - ---------- ----------
3 c 3 3
1 a 15 11
2 b 9 7
If you have non-null and different values in weeks you can use something like:
SQL> with t (id, code, week1, week2, week3, week4)
2 as (
3 select 1, 'a', 10, 15, 11, 8 from dual union all
4 select 2, 'b', 7, 4, 2, 9 from dual union all
5 select 3, 'c', 3, 2, 1, 0 from dual
6 )
7 select id, code
8 , greatest(week1, week2, week3, week4) first_top
9 , greatest(
10 case when week1 < greatest(week1, week2, week3, week4) then week1 else -1e28 end,
11 case when week2 < greatest(week1, week2, week3, week4) then week2 else -1e28 end,
12 case when week3 < greatest(week1, week2, week3, week4) then week3 else -1e28 end,
13 case when week4 < greatest(week1, week2, week3, week4) then week4 else -1e28 end
14 ) second_top
15 from t
16 /
ID C FIRST_TOP SECOND_TOP
---------- - ---------- ----------
1 a 15 11
2 b 9 7
3 c 3 2
But to get the right solution more details are required.
Answering my question...
select * from(
select * from(
select week1money col from dual
union
select week2money col from dual
union
select week3money col from dual
union
select week4money col from dual
) order by col desc
) where rownum < 3
Using GREATESTS() also may help.

Ranking with Duplicates

I am using Oracle Apex and I am ranking call speed. I am almost getting the results I am after. The only issue that I have faced is that when the rank function comes across duplicate values, by default they both get assigned the lowest Rank. e.g.
Rank Call Speed
1 65
2 72
3 92
4 102
4 102
4 102
4 102
4 102
9 113
10 154
11 201
12 352
Is there anyway to have the 4's represent as 8's (the highest rank of the duplicates)
One way of doing this is by using the ranking Descending and then subtracting this from the highest rank + 1. this works but seems like an unnecessary step.
Any help will be much appreciated
Kind of a strange thing to do, but I would do something like:
with data as (
select 65 call_speed from dual union all
select 72 call_speed from dual union all
select 92 call_speed from dual union all
select 102 call_speed from dual connect by level <= 5 union all
select 113 call_speed from dual union all
select 154 call_speed from dual union all
select 201 call_speed from dual union all
select 352 call_speed from dual
)
select
rank() over (order by call_speed) + count(*) over (partition by call_speed) - 1 rank,
call_speed
from data;
Which gives you:
RANK CALL_SPEED
---------- ----------
1 65
2 72
3 92
8 102
8 102
8 102
8 102
8 102
9 113
10 154
11 201
12 352
Just an alternative, for no reason at all, except maybe to avoid any memory overhead (?) from doing a partitioned count:
with data as (
select 65 call_speed from dual union all
select 72 call_speed from dual union all
select 92 call_speed from dual union all
select 102 call_speed from dual connect by level <= 5 union all
select 113 call_speed from dual union all
select 154 call_speed from dual union all
select 201 call_speed from dual union all
select 352 call_speed from dual
)
select
count(*) over () + 1 - rank() over (order by call_speed desc) rank,
call_speed
from data
order by call_speed;

Oracle Calculation Involving Results of Another Calculation

First off, I'm a total Oracle noob although I'm very familiar with SQL. I have a single cost column. I need to calculate the total cost, the percentage of the total cost, and then a running sum of the percentages. I'm having trouble with the running sum of percentages because the only way I can think to do this uses nested SUM functions, which isn't allowed.
Here's what works:
SELECT cost, SUM(cost) OVER() AS total, cost / SUM(cost) OVER() AS per
FROM my_table
ORDER BY cost DESC
Here's what I'm trying to do that doesn't work:
SELECT cost, SUM(cost) OVER() AS total, cost / SUM(cost) OVER() AS per,
SUM(cost/SUM(cost) OVER()) OVER(cost) AS per_sum
FROM my_table
ORDER BY cost DESC
Am I just going about it wrong, or is what I'm trying to do just not possible? By the way I'm using Oracle 10g. Thanks in advance for any help.
You don't need the order by inside that inline view, especially since the outer select is doing an order by the order way around. Also, cost / SUM(cost) OVER () equals RATIO_TO_REPORT(cost) OVER ().
An example:
SQL> create table my_table(cost)
2 as
3 select 10 from dual union all
4 select 20 from dual union all
5 select 5 from dual union all
6 select 50 from dual union all
7 select 60 from dual union all
8 select 40 from dual union all
9 select 15 from dual
10 /
Table created.
Your initial query:
SQL> SELECT cost, SUM(cost) OVER() AS total, cost / SUM(cost) OVER() AS per
2 FROM my_table
3 ORDER BY cost DESC
4 /
COST TOTAL PER
---------- ---------- ----------
60 200 .3
50 200 .25
40 200 .2
20 200 .1
15 200 .075
10 200 .05
5 200 .025
7 rows selected.
Quassnoi's query contains a typo:
SQL> SELECT cost, total, per, SUM(running) OVER (ORDER BY cost)
2 FROM (
3 SELECT cost, SUM(cost) OVER() AS total, cost / SUM(cost) OVER() AS per
4 FROM my_table
5 ORDER BY
6 cost DESC
7 )
8 /
SELECT cost, total, per, SUM(running) OVER (ORDER BY cost)
*
ERROR at line 1:
ORA-00904: "RUNNING": invalid identifier
And if I correct that typo. It gives the right results, but wrongly sorted (I guess):
SQL> SELECT cost, total, per, SUM(per) OVER (ORDER BY cost)
2 FROM (
3 SELECT cost, SUM(cost) OVER() AS total, cost / SUM(cost) OVER() AS per
4 FROM my_table
5 ORDER BY
6 cost DESC
7 )
8 /
COST TOTAL PER SUM(PER)OVER(ORDERBYCOST)
---------- ---------- ---------- -------------------------
5 200 .025 .025
10 200 .05 .075
15 200 .075 .15
20 200 .1 .25
40 200 .2 .45
50 200 .25 .7
60 200 .3 1
7 rows selected.
I think this is the one you are looking for:
SQL> select cost
2 , total
3 , per
4 , sum(per) over (order by cost desc)
5 from ( select cost
6 , sum(cost) over () total
7 , ratio_to_report(cost) over () per
8 from my_table
9 )
10 order by cost desc
11 /
COST TOTAL PER SUM(PER)OVER(ORDERBYCOSTDESC)
---------- ---------- ---------- -----------------------------
60 200 .3 .3
50 200 .25 .55
40 200 .2 .75
20 200 .1 .85
15 200 .075 .925
10 200 .05 .975
5 200 .025 1
7 rows selected.
Regards,
Rob.
SELECT cost, total, per, SUM(per) OVER (ORDER BY cost)
FROM (
SELECT cost, SUM(cost) OVER() AS total, cost / SUM(cost) OVER() AS per
FROM my_table
)
ORDER BY
cost DESC

Resources