下面是一个答案,其中一些边界案例已添加到数据集中。
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
您是否尝试过任何东西,除了要求我们做所有的工作适合你? – JNK 2012-02-17 19:15:07
如果您要给我们提供数据,您至少应该提供表创建脚本:p – jcolebrand 2012-02-17 19:36:23
该计算无法通过单个SQL查询实现吗?这就是我正在寻找的。 – 2012-02-17 20:13:45