从表中查找中值,按日期分组SQLServer

时间:2016-12-05 18:39:19

标签: sql-server group-by median

我想解决一个复杂的问题。请耐心等待,随时提出任何问题。我对SQL很陌生,对此有困难......

我需要计算一组值的中位数。现在这些值没有在表格中给出。这些值来自基于按日期分组的每小时出现的表格。

这里是汇总数据的示例表。

   CREATE TABLE Table22(
   Request_Number BIGINT  NOT NULL
  ,Request_Received_Date DATETIME  NOT NULL
);
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (2016311446,'8/9/16 9:56');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20163612157,'9/6/16 9:17');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (2016384250,'9/12/16 14:52');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20162920101,'4/19/16 8:11');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (2016418170,'10/6/16 12:28');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (2016392953,'9/6/16 12:39');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20164123416,'10/6/16 15:05');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (2016335972,'8/9/16 7:49');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20162622951,'9/6/16 9:57');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20163913504,'9/6/16 9:47');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20163211326,'9/6/16 12:38');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20163610132,'8/30/16 16:34');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20164119560,'10/6/16 15:53');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (2016334416,'8/10/16 11:06');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20164320028,'10/6/16 15:27');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (20163515193,'8/24/16 19:50');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (2016159834,'4/19/16 13:21');
INSERT INTO Table22(Request_Number,Request_Received_Date) VALUES (2016178443,'4/19/16 13:05');

该表有2列:Request_Number和Request_Received_Date。 Request_Number不是唯一的,并且是无关紧要的。我正在寻找在该日期(24小时)内特定日期和每小时收到的请求数。每次有日期条目时,都会被计为一次(TicketCount)。我可以使用COUNT语句来计算来自Request_received_date的*和按日期和小时分组。

我做到了这一点并在我的脚本中创建了一个临时表:

CREATE TABLE #z (ForDate date, OnHour int, TicketCount int)
INSERT INTO #z (ForDate, OnHour, TicketCount)           
SELECT  CAST(Request_received_date as DATE) AS 'ForDate',
                DATEPART(hh, request_received_date) AS 'OnHour', 
                COUNT(*) AS TicketCount /*Hourly Ticket Count Column*/
                FROM Table22
                GROUP BY CAST(request_received_date as DATE), DATEPART(hh, request_received_date)
                ORDER BY ForDate Desc, OnHour ASC

SELECT * FROM #z order by ForDate Desc, OnHour ASC

现在我最难找到每天计数的中位数。我已经尝试了许多不同的中位数计算公式,并且能够使它们大多数起作用。可以在此处找到许多不同的中值计算示例 https://sqlperformance.com/2012/08/t-sql-queries/median

我喜欢这段脚本来查找中位数。查找中位数的脚本很简单。但它找到了Request_Received_Date的所有值的中位数。我无法在这里找到使用group by date子句的方法。

DECLARE @Median DECIMAL (12,2); 

SELECT @Median = (
    (SELECT MAX(TicketCount) FROM 
    (SELECT TOP 50 PERCENT TicketCount FROM #z ORDER BY TicketCount) AS BottomHalf)
    +
    (SELECT MIN(TicketCount) FROM 
    (SELECT TOP 50 PERCENT TicketCount FROM #z ORDER BY TicketCount DESC) AS TopHalf))/2; 

SELECT @Median  

任何帮助都将非常感激。

预期结果如下:

ForDate   Median
10/6/2016   2
9/12/2016   1
9/6/2016    2.5
8/30/2016   1
8/24/2016   1
8/10/2016   1
8/9/2016    1
4/19/2016   1.5

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样? (仅在使用SQL Server 2012或更高版本时才适用)

SELECT DISTINCT ForDate, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY TicketCount) OVER (PARTITION BY ForDate) AS Median
FROM #z;

简而言之,SQL-Server有两种计算中位数的方法,你可以在这里阅读:https://msdn.microsoft.com/en-us/library/hh231327.aspx

在这种情况下,您可以将它们与此处的代码进行比较:

SELECT DISTINCT
    ForDate
    , PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY TicketCount) OVER (PARTITION BY ForDate) AS MedianDisc
    , PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY TicketCount) OVER (PARTITION BY ForDate) AS MedianCont
FROM
    #z;