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
是否可以拥有第二张桌子?我不知道该怎么做。 rownumber的滚动平均值,间隔为4.例如,RowNumber 1-5的平均值是所有具有Rownumber的Measure的平均值,范围为1-5。谢谢!
答案 0 :(得分:0)
是的,您可以通过使用avg分析功能轻松实现这一目标,例如:
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。你没有提到任何分组(例如,每天,每批次等),我使用了rownumber列进行排序 - 也许它应该是测量列?)。如果您的要求比您所说的要复杂,则需要适当修改over ()
条款。
通过附加说明澄清您的要求,您可以将窗口子句修改为范围,而不是之间的行,如下所示:
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
请注意,我已经包含了“rn”列,因为我不确定您是否要过滤掉“重复”的rownumber行 - 如果这样做,那么您需要添加外部查询在rn = 1上过滤。
N.B。如果您已经包含了您希望在滚动平均列中看到的实际输出值而不仅仅是逻辑,那将是有用的,这样我们就可以将结果与它进行比较。