2016-12-05 133 views
0

我有一個複雜的問題,我試圖解決。請耐心等待,隨時提出任何問題。我是相當新的SQL和有這個困難...從表中查找中間值,按日期分組SQLServer

我需要計算一組值的中位數。現在,這些值不會在表格中給出。這些值是根據按日期分組的每小時發生的表派生的。

下面是從中彙總數據的示例表。

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 

回答

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; 
+0

我更仔細地更新了答案後,我更仔細地閱讀你的問題,所以你可能想再試一次。 – DVT

+0

感謝您更新答案。我也跑了這個。我沒有得到正確的中值。日期條目也是重複的。我不能在這個語句中使用GROUP BY子句。根據我的理解,結果應該和我發佈的表格一樣。有什麼建議麼? 我不熟悉percentile_disc語句。我會在此期間查看,看看它爲什麼不起作用。謝謝 –

+0

@FariyaFarhad再試一次。 – DVT