2012-02-17 33 views
-3

數據庫:SQL Server 2008 R2的如何在連續的日期範圍計算的平均運行

表結構

  location, date,      temperature 

      NY  2011-12-06 21:07:00 -05:00 20 
      NY  2011-12-06 21:08:00 -05:00 21 
      NY  2011-12-06 21:09:00 -05:00 22 
      NY  2011-12-06 21:10:00 -05:00 23 
      NY  2011-12-06 21:11:00 -05:00 24 <- Minute breaks here 
      NY  2011-12-06 21:22:00 -05:00 27 
      NY  2011-12-06 21:23:00 -05:00 25 
      NY  2011-12-06 21:24:00 -05:00 26 
      NY  2011-12-06 21:25:00 -05:00 25 
      NY  2011-12-06 21:26:00 -05:00 26 
      NY  2011-12-06 21:27:00 -05:00 24 <- Minute breaks here(NY ends) 
      MA  2011-12-06 21:07:00 -05:00 21 
      MA  2011-12-06 21:08:00 -05:00 22 
      MA  2011-12-06 21:09:00 -05:00 24 
      MA  2011-12-06 21:10:00 -05:00 23 
      MA  2011-12-06 21:11:00 -05:00 22 <- Minute breaks here 
      MA  2011-12-06 21:22:00 -05:00 22 
      MA  2011-12-06 21:23:00 -05:00 25 
      MA  2011-12-06 21:24:00 -05:00 26 
      MA  2011-12-06 21:25:00 -05:00 24 
      MA  2011-12-06 21:26:00 -05:00 25 
      MA  2011-12-06 21:27:00 -05:00 29 

要求:該表有一個多列名爲running_average_temp 。在日期的連續分鐘內計算溫度的運行平均值的最佳方法是什麼? (即每個位置的連續分鐘讀數都是一組數據,需要爲該組中的每一行計算運行平均值)該表具有更多位置。

(發佈要求:該表每小時得到一次輸入,新進入的行可能會持續現有行的分鐘數,也可能是一組新的讀數,計算最佳方式是什麼每小時出現一組新的運行平均值。)

+5

您是否嘗試過任何東西,除了要求我們做所有的工作適合你? – JNK 2012-02-17 19:15:07

+2

如果您要給我們提供數據,您至少應該提供表創建腳本:p – jcolebrand 2012-02-17 19:36:23

+0

該計算無法通​​過單個SQL查詢實現嗎?這就是我正在尋找的。 – 2012-02-17 20:13:45

回答

0

下面是一個答案,其中一些邊界案例已添加到數據集中。

DECLARE @temps TABLE 
    (
    location varchar(2) NOT NULL, 
    date datetime NOT NULL, 
    temperature tinyint NOT NULL 
    ) 

INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:07:00',20) 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:08:00',21) 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:09:00',22) 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:10:00',23) 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:11:00',24) --<- Minute breaks here 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:22:00',27) 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:23:00',25) 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:24:00',26) 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:25:00',25) 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:26:00',26) 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 21:27:00',24) --<- Minute breaks here 
INSERT INTO @temps (location, date, temperature) VALUES ('NY', '2011-12-06 22:00:00',28) --<- Minute breaks here 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:07:00',21) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:08:00',22) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:09:00',24) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:10:00',23) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:11:00',22) --<- Minute breaks here 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:22:00',22) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:23:00',25) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:24:00',26) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:25:00',24) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:26:00',25) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 21:27:00',29) 
INSERT INTO @temps (location, date, temperature) VALUES ('MA', '2011-12-06 22:00:00',32)--<- Minute breaks here 

;WITH TempSets (RowNumber, location, date, temperature, nextminutetemperature, previousminutetemperature) 
AS 
(
SELECT 
    ROW_NUMBER() OVER (ORDER BY T.location ASC, T.date ASC) AS RowNumber, 
    T.location, 
    T.date, 
    T.temperature, 
    T1.temperature AS nextminutetemperature, 
    T2.temperature AS previousminutetemperature 
FROM 
    @temps T 
    LEFT JOIN @temps T1 ON 
     T1.location = T.location 
     AND T1.date = DATEADD(minute, +1, T.date) 
    LEFT JOIN @temps T2 ON 
     T2.location = T.location 
     AND T2.date = DATEADD(minute, -1, T.date) 
) 

SELECT 
    Result.location, 
    MIN(Result.date) AS starttime, 
    MAX(Result.date) AS endtime, 
    COUNT(Result.groupidentifier) AS numberofreadings, 
    AVG(Result.temperature) AS averagetemperature 
FROM 
(
SELECT 
    (
    CASE 
     WHEN 
      TempSets.previousminutetemperature IS NULL 
      AND TempSets.nextminutetemperature IS NULL 
      THEN TempSets.rownumber 
     ELSE 
      (
      SELECT 
       MIN(GroupNumber.rownumber) 
      FROM 
       TempSets AS GroupNumber 
      WHERE 
       GroupNumber.location = TempSets.location 
       AND GroupNumber.rownumber >= TempSets.rownumber 
       AND GroupNumber.nextminutetemperature IS NULL 
      ) 
    END 
    ) AS groupidentifier, 
    TempSets.rownumber, 
    TempSets.location, 
    TempSets.date, 
    TempSets.temperature, 
    TempSets.nextminutetemperature, 
    TempSets.previousminutetemperature 
FROM 
    TempSets 
) AS Result 
GROUP BY 
    Result.groupidentifier, 
    Result.location 
ORDER BY 
    Result.location ASC, 
    MIN(Result.date) ASC 

結果:

location starttime    endtime     numberofreadings averagetemperature 
-------- ----------------------- ----------------------- ---------------- ------------------ 
MA  2011-12-06 21:07:00.000 2011-12-06 21:11:00.000 5    22 
MA  2011-12-06 21:22:00.000 2011-12-06 21:27:00.000 6    25 
MA  2011-12-06 22:00:00.000 2011-12-06 22:00:00.000 1    32 
NY  2011-12-06 21:07:00.000 2011-12-06 21:11:00.000 5    22 
NY  2011-12-06 21:22:00.000 2011-12-06 21:27:00.000 6    25 
NY  2011-12-06 22:00:00.000 2011-12-06 22:00:00.000 1    28 
+0

我在Tsql中編寫了這個程序。這有助於瞭解如何使用SQL來解決問題。感謝您採用這種方法,我可以嘗試將其發展爲運行平均計算。 – 2012-02-17 20:46:21

+0

歡迎來到計算器。 – 2012-02-17 21:04:23

+0

另一種方法是找到所有邊界情況,其中沒有行的時間少於1分鐘,或者沒有行的時間多於1分鐘,並通過使用邊界位置/日期作爲下限/上限進行解決的子集。只是思考的食物。 – 2012-02-17 23:12:34