JonWay JonWay - 3 months ago 8
SQL Question

Rolling 95 percentile and Median for each Month

Hi Guys I have the following data and I want to calculate the 95 percentile and Median for each of the month. All the example that I have looked at only calculate a single value result set. I want my answer to look as follows

YearMonth |95_percentile_DaysWaith| MedianDaysWaith
2014/11
2014/12
2014/13
2014/14
2014/15


--The table below are the data

CREATE TABLE ##Waiter
(YearMonth VARCHAR(8), DaysWaith INT, RefuseToWait INT)
INSERT INTO ##Waiter
VALUES
('2014/11',173,0),('2014/12',239,0),('2015/01',305,0),('2015/02',238,0),('2015/03',216,0),('2015/04',190,0),('2015/05',245,0),
('2014/11',224,0),('2014/12',421,0),('2015/01',165,0),('2014/11',213,0),('2014/12',236,0),('2015/01',54,0),('2015/02',283,0),
('2015/03',11,0),('2015/04',123,0),('2015/05',106,0),('2015/06',160,0),('2014/11',160,0),('2015/09',56,0),('2015/10',30,0),
('2014/12',150,0),('2015/01',206,0),('2015/02',119,0),('2015/03',76,0),('2015/04',42,0),('2015/05',54,1),('2015/06',262,0),
('2015/07',228,0),('2015/08',126,0),('2015/09',136,0),('2014/11',78,0),('2014/12',239,0),('2015/01',224,0),
('2015/02',274,0),('2014/13',116,0),('2014/14',142,0),('2015/06',210,0),('2015/07',157,0),('2015/08',202,0),
('2014/13',172,0),('2014/14',232,0),('2015/02',17,0),('2014/13',65,0),('2014/14',93,0),('2015/07',81,0),('2015/08',11,0),
('2015/09',29,0),('2015/10',51,0),('2015/11',193,0),('2015/12',302,0),('2014/13',62,0),('2014/14',86,0),('2015/10',722,0),
('2015/11',143,1),('2015/12',214,0),('2015/13',118,1),('2015/14',84,0),('2015/15',16,0),('2015/16',170,0),('2015/17',114,0),
('2015/18',503,0),('2014/13',118,0),('2014/14',78,0),('2015/03',187,0),('2015/04',96,0),('2014/15',42,0),('2014/16',128,0),
('2015/11',155,0),('2015/12',167,0),('2015/13',35,0),('2015/14',190,0),('2015/15',98,0),('2014/15',112,0),('2014/16',154,0),
('2015/03',64,0),('2014/15',294,0),('2014/16',250,0),('2015/13',45,0),('2015/14',176,0),('2015/15',172,0),('2015/16',356,0),
('2015/17',147,0),('2015/18',237,0),('2014/15',144,0),('2014/16',122,0),('2015/19',236,0),('2015/20',71,0),('2015/21',245,0),
('2015/22',79,0),('2015/23',238,0),('2015/24',98,0),('2015/25',81,0),('2015/26',234,0),('2015/27',147,0),('2014/15',178,1),
('2014/16',143,0),('2015/05',239,0),('2015/06',237,0),('2014/17',206,0),('2014/18',124,0),('2015/16',239,0),('2015/17',85,0),
('2015/18',284,0),('2015/19',222,0),('2015/20',238,0),('2014/17',56,0),('2014/18',145,0),('2015/04',175,0),('2014/17',268,1),
('2014/18',163,0),('2015/19',149,0),('2015/20',147,0),('2015/21',144,0),('2015/22',143,0),('2015/23',239,0),('2015/24',212,0),
('2014/17',220,0),('2014/18',154,0),('2015/28',236,0),('2015/29',182,0),('2015/30',213,0),('2015/31',141,0),('2015/32',238,0),
('2015/33',40,0),('2015/34',169,0),('2015/35',225,0),('2015/36',202,0),('2014/17',200,0),('2014/18',124,0),('2015/07',56,0),
('2015/08',237,0),('2014/19',31,0),('2014/20',237,0),('2015/21',236,0),('2015/22',218,0),('2015/23',89,0),('2015/24',182,0),
('2015/25',194,0),('2014/19',37,0),('2014/20',239,0),('2015/05',200,0),('2014/19',239,0),('2014/20',179,0),('2015/25',184,0),
('2015/26',104,1),('2015/27',56,0),('2015/28',291,0),('2015/29',234,0),('2015/30',166,0),('2014/19',123,0),('2014/20',292,0),('2015/37',232,1),
('2015/38',52,0),('2015/39',226,1),('2015/40',221,0),('2015/41',438,0),('2015/42',118,0),('2015/43',207,0),('2015/44',154,0),
('2015/45',152,0),('2014/19',165,0),('2014/20',221,0),('2015/09',137,0),('2015/10',222,0),('2014/21',220,0),('2014/22',118,0),
('2015/26',191,0),('2015/27',165,0),('2015/28',57,0),('2015/29',158,0),('2015/30',149,0),('2014/21',164,0),('2014/22',128,0),
('2015/06',388,0),('2014/21',364,0),('2014/22',154,1),('2015/31',151,0),('2015/32',229,0),('2015/33',181,0),('2015/34',343,0),
('2015/35',107,0),('2015/36',168,0),('2014/21',235,0),('2014/22',270,0),('2015/46',160,0)


I can do it in excel but unable to do it in SQL Server. Any question please asks. Many thanks in advance

Answer

For MSSQL 2012 or higher:

SELECT 
    *,
    PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY w.DaysWaith)  OVER (PARTITION BY w.YearMonth) AS Percentile95,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY w.DaysWaith)  OVER (PARTITION BY w.YearMonth) as Median
FROM ##Waiter AS w
ORDER BY w.YearMonth

For MSSQL 2008 I found this post, were you can find missing function PERCENTILE_CONT.

PERCENTILE_CONT from MSDN

Comments