如何获取上一年到当年的月度数据?

时间:2017-10-26 08:07:23

标签: sql oracle analytics

我正在尝试计算度量的中位数,但我需要将上一年/月/工作日的中位数数据显示在当前数据上。

date        wday    admns   median  result
28/10/2016  Friday  7      37.5     0
21/10/2016  Friday  34     37.5     0
7/10/2016   Friday  41     37.5     0
14/10/2016  Friday  63     37.5     0

20/10/2017  Friday  23     49       37.5
27/10/2017  Friday  44     49       37.5
6/10/2017   Friday  54     49       37.5
13/10/2017  Friday  63     49       37.5

在结果栏中,我需要将上一年/月/日的中位数显示到当前年份。对于2015年,没有数据,因此2016年数据的结果字段为0。

我使用此查询来获取中位数

with a as (select dt,weekday,admns,extract(year from dt) year,to_char(trunc(sysdate,'MM'),'Month') month from med_drop)
select dt, weekday, admns , year,month,
       median(admns) over ( partition by year,weekday) export_table
from a;

测试数据

CREATE TABLE export_table
   (DT DATE, 
    WEEKDAY VARCHAR2(10), 
    ADMNS NUMBER
   );


Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('01/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',10);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('02/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',12);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('03/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Tuesday',15);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('04/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Wednesday',52);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('05/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Thursday',23);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('06/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Friday',54);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('07/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Saturday',78);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('08/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',66);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('09/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',5);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('10/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Tuesday',4);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('11/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Wednesday',78);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('12/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Thursday',99);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('13/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Friday',63);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('14/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Saturday',71);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('15/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',63);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('16/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',89);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('17/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Tuesday',23);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('18/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Wednesday',45);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('19/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Thursday',87);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('20/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Friday',23);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('21/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Saturday',12);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('22/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',11);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('23/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',63);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('24/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Tuesday',93);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('25/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Wednesday',23);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('26/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Thursday',88);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('27/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Friday',44);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('28/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Saturday',441);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('29/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',66);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('30/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',23);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('31/OCT/2017 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Tuesday',16);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('01/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Saturday',55);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('02/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',67);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('03/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',41);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('04/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Tuesday',85);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('05/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Wednesday',25);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('06/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Thursday',63);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('07/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Friday',41);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('08/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Saturday',87);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('09/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',46);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('10/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',14);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('11/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Tuesday',74);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('12/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Wednesday',36);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('13/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Thursday',74);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('14/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Friday',63);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('15/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Saturday',14);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('16/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',96);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('17/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',31);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('18/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Tuesday',41);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('19/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Wednesday',23);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('20/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Thursday',96);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('21/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Friday',34);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('22/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Saturday',74);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('23/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',63);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('24/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',53);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('25/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Tuesday',76);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('26/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Wednesday',96);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('27/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Thursday',145);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('28/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Friday',7);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('29/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Saturday',8);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('30/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Sunday',4);
Insert into EXPORT_TABLE (DT,WEEKDAY,ADMNS) values (to_date('31/OCT/2016 00:00:00','DD/MON/YYYY HH24:MI:SS'),'Monday',6);

2 个答案:

答案 0 :(得分:0)

来自MEDIAN documentation

  

您可以使用MEDIAN作为分析功能。您只能在其OVER子句中指定query_partition_clause。

因此,您无法在分析查询中指定窗口子句,只能将其过滤到此/去年。相反,您可以使用相关的子查询:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE export_table(
  DT DATE, 
  WEEKDAY VARCHAR2(9) GENERATED ALWAYS AS ( CAST( TRIM( TO_CHAR( dt, 'Day' ) ) AS VARCHAR2(9) ) ) VIRTUAL, 
  ADMNS NUMBER
);

Insert into EXPORT_TABLE (DT,ADMNS)
SELECT DATE '2017-10-01',10 FROM DUAL UNION ALL
SELECT DATE '2017-10-02',12 FROM DUAL UNION ALL
SELECT DATE '2017-10-03',15 FROM DUAL UNION ALL
SELECT DATE '2017-10-04',52 FROM DUAL UNION ALL
SELECT DATE '2017-10-05',23 FROM DUAL UNION ALL
SELECT DATE '2017-10-06',54 FROM DUAL UNION ALL
SELECT DATE '2017-10-07',78 FROM DUAL UNION ALL
SELECT DATE '2017-10-08',66 FROM DUAL UNION ALL
SELECT DATE '2017-10-09',5 FROM DUAL UNION ALL
SELECT DATE '2017-10-10',4 FROM DUAL UNION ALL
SELECT DATE '2017-10-11',78 FROM DUAL UNION ALL
SELECT DATE '2017-10-12',99 FROM DUAL UNION ALL
SELECT DATE '2017-10-13',63 FROM DUAL UNION ALL
SELECT DATE '2017-10-14',71 FROM DUAL UNION ALL
SELECT DATE '2017-10-15',63 FROM DUAL UNION ALL
SELECT DATE '2017-10-16',89 FROM DUAL UNION ALL
SELECT DATE '2017-10-17',23 FROM DUAL UNION ALL
SELECT DATE '2017-10-18',45 FROM DUAL UNION ALL
SELECT DATE '2017-10-19',87 FROM DUAL UNION ALL
SELECT DATE '2017-10-20',23 FROM DUAL UNION ALL
SELECT DATE '2017-10-21',12 FROM DUAL UNION ALL
SELECT DATE '2017-10-22',11 FROM DUAL UNION ALL
SELECT DATE '2017-10-23',63 FROM DUAL UNION ALL
SELECT DATE '2017-10-24',93 FROM DUAL UNION ALL
SELECT DATE '2017-10-25',23 FROM DUAL UNION ALL
SELECT DATE '2017-10-26',88 FROM DUAL UNION ALL
SELECT DATE '2017-10-27',44 FROM DUAL UNION ALL
SELECT DATE '2017-10-28',441 FROM DUAL UNION ALL
SELECT DATE '2017-10-29',66 FROM DUAL UNION ALL
SELECT DATE '2017-10-30',23 FROM DUAL UNION ALL
SELECT DATE '2017-10-31',16 FROM DUAL UNION ALL
SELECT DATE '2016-10-01',55 FROM DUAL UNION ALL
SELECT DATE '2016-10-02',67 FROM DUAL UNION ALL
SELECT DATE '2016-10-03',41 FROM DUAL UNION ALL
SELECT DATE '2016-10-04',85 FROM DUAL UNION ALL
SELECT DATE '2016-10-05',25 FROM DUAL UNION ALL
SELECT DATE '2016-10-06',63 FROM DUAL UNION ALL
SELECT DATE '2016-10-07',41 FROM DUAL UNION ALL
SELECT DATE '2016-10-08',87 FROM DUAL UNION ALL
SELECT DATE '2016-10-09',46 FROM DUAL UNION ALL
SELECT DATE '2016-10-10',14 FROM DUAL UNION ALL
SELECT DATE '2016-10-11',74 FROM DUAL UNION ALL
SELECT DATE '2016-10-12',36 FROM DUAL UNION ALL
SELECT DATE '2016-10-13',74 FROM DUAL UNION ALL
SELECT DATE '2016-10-14',63 FROM DUAL UNION ALL
SELECT DATE '2016-10-15',14 FROM DUAL UNION ALL
SELECT DATE '2016-10-16',96 FROM DUAL UNION ALL
SELECT DATE '2016-10-17',31 FROM DUAL UNION ALL
SELECT DATE '2016-10-18',41 FROM DUAL UNION ALL
SELECT DATE '2016-10-19',23 FROM DUAL UNION ALL
SELECT DATE '2016-10-20',96 FROM DUAL UNION ALL
SELECT DATE '2016-10-21',34 FROM DUAL UNION ALL
SELECT DATE '2016-10-22',74 FROM DUAL UNION ALL
SELECT DATE '2016-10-23',63 FROM DUAL UNION ALL
SELECT DATE '2016-10-24',53 FROM DUAL UNION ALL
SELECT DATE '2016-10-25',76 FROM DUAL UNION ALL
SELECT DATE '2016-10-26',96 FROM DUAL UNION ALL
SELECT DATE '2016-10-27',145 FROM DUAL UNION ALL
SELECT DATE '2016-10-28',7 FROM DUAL UNION ALL
SELECT DATE '2016-10-29',8 FROM DUAL UNION ALL
SELECT DATE '2016-10-30',4 FROM DUAL UNION ALL
SELECT DATE '2016-10-31',6 FROM DUAL;

查询1

select dt,
       weekday,
       admns,
       ( SELECT MEDIAN( admns )
         FROM   export_table t
         WHERE  e.weekday = t.weekday
         AND    t.dt BETWEEN TRUNC( e.dt, 'MM' )
                         AND LAST_DAY( e.dt )
       ) AS this_years_median_for_month,
       ( SELECT MEDIAN( admns )
         FROM   export_table t
         WHERE  e.weekday = t.weekday
         AND    t.dt BETWEEN ADD_MONTHS( TRUNC( e.dt, 'MM' ), -12 )
                         AND ADD_MONTHS( LAST_DAY( e.dt ), -12 )
       ) AS last_years_median_for_month
from   export_table e
where  EXTRACT( MONTH FROM dt ) = EXTRACT( MONTH FROM SYSDATE )
ORDER BY dt

<强> Results

|                   DT |   WEEKDAY | ADMNS | THIS_YEARS_MEDIAN_FOR_MONTH | LAST_YEARS_MEDIAN_FOR_MONTH |
|----------------------|-----------|-------|-----------------------------|-----------------------------|
| 2016-10-01T00:00:00Z |  Saturday |    55 |                          55 |                      (null) |
| 2016-10-02T00:00:00Z |    Sunday |    67 |                          63 |                      (null) |
| 2016-10-03T00:00:00Z |    Monday |    41 |                          31 |                      (null) |
| 2016-10-04T00:00:00Z |   Tuesday |    85 |                          75 |                      (null) |
| 2016-10-05T00:00:00Z | Wednesday |    25 |                        30.5 |                      (null) |
| 2016-10-06T00:00:00Z |  Thursday |    63 |                          85 |                      (null) |
| 2016-10-07T00:00:00Z |    Friday |    41 |                        37.5 |                      (null) |
| 2016-10-08T00:00:00Z |  Saturday |    87 |                          55 |                      (null) |
| 2016-10-09T00:00:00Z |    Sunday |    46 |                          63 |                      (null) |
| 2016-10-10T00:00:00Z |    Monday |    14 |                          31 |                      (null) |
| 2016-10-11T00:00:00Z |   Tuesday |    74 |                          75 |                      (null) |
| 2016-10-12T00:00:00Z | Wednesday |    36 |                        30.5 |                      (null) |
| 2016-10-13T00:00:00Z |  Thursday |    74 |                          85 |                      (null) |
| 2016-10-14T00:00:00Z |    Friday |    63 |                        37.5 |                      (null) |
| 2016-10-15T00:00:00Z |  Saturday |    14 |                          55 |                      (null) |
| 2016-10-16T00:00:00Z |    Sunday |    96 |                          63 |                      (null) |
| 2016-10-17T00:00:00Z |    Monday |    31 |                          31 |                      (null) |
| 2016-10-18T00:00:00Z |   Tuesday |    41 |                          75 |                      (null) |
| 2016-10-19T00:00:00Z | Wednesday |    23 |                        30.5 |                      (null) |
| 2016-10-20T00:00:00Z |  Thursday |    96 |                          85 |                      (null) |
| 2016-10-21T00:00:00Z |    Friday |    34 |                        37.5 |                      (null) |
| 2016-10-22T00:00:00Z |  Saturday |    74 |                          55 |                      (null) |
| 2016-10-23T00:00:00Z |    Sunday |    63 |                          63 |                      (null) |
| 2016-10-24T00:00:00Z |    Monday |    53 |                          31 |                      (null) |
| 2016-10-25T00:00:00Z |   Tuesday |    76 |                          75 |                      (null) |
| 2016-10-26T00:00:00Z | Wednesday |    96 |                        30.5 |                      (null) |
| 2016-10-27T00:00:00Z |  Thursday |   145 |                          85 |                      (null) |
| 2016-10-28T00:00:00Z |    Friday |     7 |                        37.5 |                      (null) |
| 2016-10-29T00:00:00Z |  Saturday |     8 |                          55 |                      (null) |
| 2016-10-30T00:00:00Z |    Sunday |     4 |                          63 |                      (null) |
| 2016-10-31T00:00:00Z |    Monday |     6 |                          31 |                      (null) |
| 2017-10-01T00:00:00Z |    Sunday |    10 |                          63 |                          63 |
| 2017-10-02T00:00:00Z |    Monday |    12 |                          23 |                          31 |
| 2017-10-03T00:00:00Z |   Tuesday |    15 |                          16 |                          75 |
| 2017-10-04T00:00:00Z | Wednesday |    52 |                        48.5 |                        30.5 |
| 2017-10-05T00:00:00Z |  Thursday |    23 |                        87.5 |                          85 |
| 2017-10-06T00:00:00Z |    Friday |    54 |                          49 |                        37.5 |
| 2017-10-07T00:00:00Z |  Saturday |    78 |                        74.5 |                          55 |
| 2017-10-08T00:00:00Z |    Sunday |    66 |                          63 |                          63 |
| 2017-10-09T00:00:00Z |    Monday |     5 |                          23 |                          31 |
| 2017-10-10T00:00:00Z |   Tuesday |     4 |                          16 |                          75 |
| 2017-10-11T00:00:00Z | Wednesday |    78 |                        48.5 |                        30.5 |
| 2017-10-12T00:00:00Z |  Thursday |    99 |                        87.5 |                          85 |
| 2017-10-13T00:00:00Z |    Friday |    63 |                          49 |                        37.5 |
| 2017-10-14T00:00:00Z |  Saturday |    71 |                        74.5 |                          55 |
| 2017-10-15T00:00:00Z |    Sunday |    63 |                          63 |                          63 |
| 2017-10-16T00:00:00Z |    Monday |    89 |                          23 |                          31 |
| 2017-10-17T00:00:00Z |   Tuesday |    23 |                          16 |                          75 |
| 2017-10-18T00:00:00Z | Wednesday |    45 |                        48.5 |                        30.5 |
| 2017-10-19T00:00:00Z |  Thursday |    87 |                        87.5 |                          85 |
| 2017-10-20T00:00:00Z |    Friday |    23 |                          49 |                        37.5 |
| 2017-10-21T00:00:00Z |  Saturday |    12 |                        74.5 |                          55 |
| 2017-10-22T00:00:00Z |    Sunday |    11 |                          63 |                          63 |
| 2017-10-23T00:00:00Z |    Monday |    63 |                          23 |                          31 |
| 2017-10-24T00:00:00Z |   Tuesday |    93 |                          16 |                          75 |
| 2017-10-25T00:00:00Z | Wednesday |    23 |                        48.5 |                        30.5 |
| 2017-10-26T00:00:00Z |  Thursday |    88 |                        87.5 |                          85 |
| 2017-10-27T00:00:00Z |    Friday |    44 |                          49 |                        37.5 |
| 2017-10-28T00:00:00Z |  Saturday |   441 |                        74.5 |                          55 |
| 2017-10-29T00:00:00Z |    Sunday |    66 |                          63 |                          63 |
| 2017-10-30T00:00:00Z |    Monday |    23 |                          23 |                          31 |
| 2017-10-31T00:00:00Z |   Tuesday |    16 |                          16 |                          75 |

答案 1 :(得分:0)

您可以使用滞后函数,而不是进行两次查询,例如

with a as (select dt, weekday, admns, extract(year from dt) year, to_char(trunc(sysdate,'MM'),'Month') month from export_table),
     b as (select dt, weekday, admns, year, month, median(admns) over (partition by year, weekday) med from a)
select dt, weekday, admns, year, month, med, lag(med, extract(day from last_day(dt)) ) over (order by dt) med_prev
from b
order by dt;

这只是使用extract(day from last_day(dt))来获取偏移量,因为样本数据集中只有两个月。 对于全年数据,您可以使用类似

的内容
lag(med, dt - add_months(dt,-12))

获取偏移量。 虽然这可能会使用2月29日之前的2月28日中位数,所以如果你不想那样你需要以不同的方式处理这个案例

这也假设数据集包含每天的数据。如果存在可能的差距,则可以使用稍微修改的a cte来填充这些差距以填充感兴趣的时段中的所有日期。