2017-07-27 644 views
0

即時通訊使用MS Sql Server 2008 R2數據庫。我試圖每隔15分鐘從數據庫中獲取數據。所以我想這個查詢MS Sql查詢檢索每15分鐘時間間隔的數據

我的表名是記錄和我有一樣的日期,溫度,壓力列

SELECT MIN([Date]) AS Rext 
    FROM Records where Date between '2017-01-27 00:00:00' and '2017-02-11 00:00:00' 
    GROUP BY DATEPART(YEAR, [Date]), 
    DATEPART(MONTH, [Date]), 
    DATEPART(DAY, [Date]), 
    DATEPART(HOUR, [Date]), 
    (DATEPART(MINUTE, [Date])/15) 
    ORDER BY Rext 

此查詢,我和15個間隔時間即時得到日期列工作差距。這是我的輸出

2017-01-27 18:45:43.000 
2017-01-27 19:00:35.000 
2017-01-27 19:15:35.000 
2017-01-27 19:30:35.000 
2017-01-27 19:45:35.000 
2017-01-27 20:00:35.000 
2017-01-27 20:15:35.000 
2017-01-27 20:30:35.000 
2017-01-27 20:45:35.000 

但是當我試圖用日期列IM GE沿檢索等欄目(溫度和壓力)輸出但間隔不匹配。我查詢像

SELECT MIN([Date]) AS Rext , Temperature, Pressure 
    FROM Records where Date between '2017-01-27 00:00:00' and '2017-02-11 00:00:00' 
    GROUP BY DATEPART(YEAR, [Date]), 
    DATEPART(MONTH, [Date]), 
    DATEPART(DAY, [Date]), 
    DATEPART(HOUR, [Date]), 
    (DATEPART(MINUTE, [Date])/15) 
    ORDER BY Rext,Temperature, Pressure 

而且我的輸出是

2017-01-27 18:45:43.000 18.1  57   
2017-01-27 18:46:43.000 17.9  57   
2017-01-27 18:47:43.000 17.9  58   
2017-01-27 18:50:35.000 18   57   
2017-01-27 19:00:35.000 18   57   
2017-01-27 19:03:35.000 17.9  57   
2017-01-27 19:15:35.000 18.1  57   
2017-01-27 19:16:35.000 18   57   
2017-01-27 19:21:35.000 17.9  57   
2017-01-27 19:30:35.000 18   57   
2017-01-27 19:31:35.000 18.1  57   
2017-01-27 19:33:35.000 17.9  57   
2017-01-27 19:38:35.000 17.9  58   
2017-01-27 19:39:34.000 17.9  56   
2017-01-27 19:40:35.000 17.8  54   
2017-01-27 19:41:35.000 17.9  53   
2017-01-27 19:42:35.000 18   49   
2017-01-27 19:43:35.000 17.9  49   
2017-01-27 19:44:35.000 18   46   
2017-01-27 19:45:35.000 17.9  43   
2017-01-27 19:46:35.000 18   42   
2017-01-27 19:47:35.000 17.9  40   
2017-01-27 19:48:34.000 18   39   
2017-01-27 19:49:35.000 18.1  37   
2017-01-27 19:50:35.000 18.3  36   
2017-01-27 19:51:35.000 18.5  34   
2017-01-27 19:52:35.000 18.6  33   
2017-01-27 19:53:35.000 18.7  31   
2017-01-27 19:54:35.000 18.8  30   
2017-01-27 19:55:35.000 19.1  29   
2017-01-27 19:56:35.000 19.3  28   
2017-01-27 19:58:35.000 19.5  27   
2017-01-27 19:59:35.000 19.6  27   
2017-01-27 20:00:35.000 19.7  26   
2017-01-27 20:01:34.000 19.5  26   
2017-01-27 20:02:35.000 19.4  26   
2017-01-27 20:04:35.000 19.5  25   
2017-01-27 20:06:35.000 19.4  25   
2017-01-27 20:07:35.000 19.3  25   
2017-01-27 20:15:35.000 19.4  25   
2017-01-27 20:16:34.000 19.3  25   
2017-01-27 20:18:35.000 19.5  25   
2017-01-27 20:20:35.000 19.5  24   
2017-01-27 20:21:35.000 19.3  24   
2017-01-27 20:22:35.000 19.4  24   
2017-01-27 20:28:35.000 19.6  24   
2017-01-27 20:30:35.000 19.6  24   
2017-01-27 20:31:35.000 19.5  24   
2017-01-27 20:35:35.000 19.4  24   
2017-01-27 20:45:35.000 19.6  24   
2017-01-27 20:47:35.000 19.5  24   
2017-01-27 21:00:35.000 19.5  24   
2017-01-27 21:01:35.000 19.6  23   
2017-01-27 21:05:47.000 19.7  23   
2017-01-27 21:14:47.000 19.8  23   
2017-01-27 21:15:47.000 19.7  23   
2017-01-27 21:16:47.000 19.8  23   
2017-01-27 21:20:47.000 19.8  22 

請任何一個幫助我。 THANK YOU

+0

快捷:當你使用多合一條件爲所有其他列(非聚合)在選擇應該來分組條款。 – SenthilPrabhu

+0

您發佈的查詢會導致錯誤。請發佈實際的查詢,生成你說你得到的結果。 –

回答

0
SELECT DATEADD(minute,DATEDIFF(minute,0,Date)/15*15,0) AS Rext , MIN(Temperature), MAX(Pressure) 
    FROM Records where Date between '2017-01-27 00:00:00' and '2017-02-11 00:00:00' 
    GROUP BY DATEADD(minute,DATEDIFF(minute,0,Date)/15*15,0), Temperature, Pressure 
    ORDER BY 1 

快捷:每當你使用聚合條件選擇所有其他列(非聚合)應在GROUP BY子句

+0

我也試過這個你對我提出的建議,但是間隔時間不正確 –

+0

儘量讓日期四捨五入,以便group by子句能夠完美地工作。 DATEADD(分,DATEDIFF(分鐘,0,DT)/ 15 * 15,0)。你可以參考https://social.msdn.microsoft.com/Forums/sqlserver/en-US/8364f9c8-65c6-4297-b9ff-5c2cf39ead4b/round-up-datetime-in-15-minute-increments?forum=ssdt – SenthilPrabhu

相關問題