I use the query below to find the median for each sector
SELECT DISTINCT Sector, PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY Value) OVER (PARTITION BY sector) AS Median FROM TABLE
The table is in the format below
Sector Date Value A 2014-08-01 1 B 2014-08-01 5 C 2014-08-01 7 A 2014-08-02 6 B 2014-08-02 5 C 2014-08-02 4 A 2014-08-03 3 B 2014-08-03 9 C 2014-08-03 6 A 2014-08-04 5 B 2014-08-04 8 C 2014-08-04 9 A 2014-08-05 5 B 2014-08-05 7 C 2014-08-05 2
So, I get the expected result below
Sector Median A 5 B 7 C 6
Now I need to change the process so that the medians are calculated, only taking into account the records before the specified date. So the new result will be
Sector Date Value A 2014-08-01 1 B 2014-08-01 5 C 2014-08-01 7 (Only 1 record each was considered for A, B and C) A 2014-08-02 3.5 B 2014-08-02 5 C 2014-08-02 5.5 (2 records each was considered for A, B and C) A 2014-08-03 3 B 2014-08-03 5 C 2014-08-03 6 (3 records each was considered for A, B and C) A 2014-08-04 4 B 2014-08-04 6.5 C 2014-08-04 6.5 (4 records each was considered for A, B and C) A 2014-08-05 5 B 2014-08-05 7 C 2014-08-05 6 (All 5 records each was considered for A, B and C)
So, this will be a kind of cumulative median. Can someone please tell me how to achieve this. My table has about 2.3 M records with approximately 1,100 records each for approximately 1,100 dates.
Please let me know if you need information.
source share