我有一张非常大的桌子,我每1分钟就有一次温度记录,我想查询的是一个趋势;类似于每个选定时期的百分比增加或百分比减少(小时或15分钟;取决于查询)
我的表看起来像(例子),如下所示
ID time temp
119950 2013-03-27 07:56:05 27.25
119951 2013-03-27 07:57:05 27.50
119952 2013-03-27 07:58:05 27.60
119953 2013-03-27 07:59:05 27.80
119954 2013-03-27 08:00:05 27.70
119955 2013-03-27 08:01:05 27.50
119956 2013-03-27 08:02:05 27.25
119957 2013-03-27 08:03:05 27.10
119958 2013-03-27 08:04:05 26.9
119959 2013-03-27 08:05:05 27.1
119960 2013-03-27 08:06:05 27.25
119961 2013-03-27 08:07:05 27.6
我相信一个趋势可以计算如下(按照link),但如果你有更好的方法,请纠正我; 取每行之间的差值然后加上然后按计数除。所以对于上面的表我们得到了
Diff
+0.25
+0.10
+0.20
-0.10
-0.20
-0.25
-0.15
-0.20
+0.20
+0.15
+0.35
最后11分钟的每分钟趋势是diff / 11的总和。最后11分钟,每分钟0.063C。
有人可以帮助我获得过去3小时每小时的百分比趋势。和每分钟趋势1小时?
答案 0 :(得分:2)
CREATE TABLE temperature_log
(ID INT NOT NULL,dt DATETIME NOT NULL, temperature DECIMAL(5,2) NOT NULL);
INSERT INTO temperature_log VALUES
(119950 ,'2013-03-27 07:56:05',27.25),
(119951 ,'2013-03-27 07:57:05', 27.50),
(119952 ,'2013-03-27 07:58:05', 27.60),
(119953 ,'2013-03-27 07:59:05', 27.80),
(119954 ,'2013-03-27 08:00:05', 27.70),
(119955 ,'2013-03-27 08:01:05', 27.50),
(119956 ,'2013-03-27 08:02:05', 27.25),
(119957 ,'2013-03-27 08:03:05', 27.10),
(119958 ,'2013-03-27 08:04:05', 26.9),
(119959 ,'2013-03-27 08:05:05', 27.1),
(119960 ,'2013-03-27 08:06:05', 27.25),
(119961 ,'2013-03-27 08:07:05', 27.6);
SELECT x.*
, x.temperature - y.temperature diff
, COUNT(*) cnt
,(x.temperature-y.temperature)/COUNT(*) trend
FROM temperature_log x
JOIN temperature_log y
ON y.id < x.id
GROUP
BY x.id;
+--------+---------------------+-------------+-------+-----+-----------+
| ID | dt | temperature | diff | cnt | trend |
+--------+---------------------+-------------+-------+-----+-----------+
| 119951 | 2013-03-27 07:57:05 | 27.50 | 0.25 | 1 | 0.250000 |
| 119952 | 2013-03-27 07:58:05 | 27.60 | 0.35 | 2 | 0.175000 |
| 119953 | 2013-03-27 07:59:05 | 27.80 | 0.55 | 3 | 0.183333 |
| 119954 | 2013-03-27 08:00:05 | 27.70 | 0.45 | 4 | 0.112500 |
| 119955 | 2013-03-27 08:01:05 | 27.50 | 0.25 | 5 | 0.050000 |
| 119956 | 2013-03-27 08:02:05 | 27.25 | 0.00 | 6 | 0.000000 |
| 119957 | 2013-03-27 08:03:05 | 27.10 | -0.15 | 7 | -0.021429 |
| 119958 | 2013-03-27 08:04:05 | 26.90 | -0.35 | 8 | -0.043750 |
| 119959 | 2013-03-27 08:05:05 | 27.10 | -0.15 | 9 | -0.016667 |
| 119960 | 2013-03-27 08:06:05 | 27.25 | 0.00 | 10 | 0.000000 |
| 119961 | 2013-03-27 08:07:05 | 27.60 | 0.35 | 11 | 0.031818 |
+--------+---------------------+-------------+-------+-----+-----------+
顺便提一下,如果你有兴趣获得每小时的平均成绩,你可以这样做......
SELECT DATE_FORMAT(x.dt,'%Y-%m-%d %h:00:00')
, AVG(x.temperature) avg_temp
FROM temperature_log x
GROUP
BY DATE_FORMAT(x.dt,'%Y-%m-%d %h:00:00');
答案 1 :(得分:0)
我知道这门课程很老,但是如果我能与您分享我的经验。 也许这对下一个人可能有用:)
我有一张很大的桌子,上面列出了我所有设备的温度(100+),我所有的设备每5秒推一次温度(一个设备有6个视野,我可以得到每个区域的温度)。
所以桌子很大。对我而言,先前的响应无法处理大量数据。 看我在做什么:
这是我的大表的架构:
CREATE TABLE `histozone` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`camera_id` INT(11) NULL DEFAULT NULL,
`Date` DATE NOT NULL,
`Time` TIME NOT NULL,
`ZoneId` INT(11) NOT NULL,
`AverageTemperature` INT(11) NOT NULL,
`MinimumTemperature` INT(11) NOT NULL,
`MaximumTemperature` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX `IDX_19E8F664B47685CD` (`camera_id`),
INDEX `datetime` (`camera_id`, `Date`, `Time`),
);
就像您看到的一样,每行分别有Date
和Time
。
DROP TEMPORARY TABLE IF EXISTS histoZoneMaxTempCamera{$cameraId};
CREATE TEMPORARY TABLE histoZoneMaxTempCamera{$cameraId} (
`id` INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
`originalid` INT(11) NOT NULL,
`date` DATE NOT NULL,
`time` TIME NOT NULL,
`zoneid` INT(11) NOT NULL,
`maximumtemperature` INT(11) NOT NULL,
INDEX (`maximumtemperature`)
) ENGINE=MEMORY;
INSERT INTO histoZoneMaxTempCamera{$cameraId} (`originalid`, `date`, `time`, `zoneid`, `maximumtemperature`)
SELECT
h.id,
h.Date,
h.Time,
h.ZoneId,
h.MaximumTemperature
FROM histozone h
INNER JOIN (
SELECT
hz.camera_id,
MAX(hz.MaximumTemperature) AS MaximumTemperature,
hz.Date,
hz.Time
FROM histozone hz
WHERE hz.camera_id = '{$cameraId}'
AND hz.Date >= '{$date}'
AND hz.Time >= '{$time}'
GROUP BY hz.Date, hz.Time
) histozoneMaxTemp
ON h.Date = histozoneMaxTemp.Date
AND h.Time = histozoneMaxTemp.Time
AND h.MaximumTemperature = histozoneMaxTemp.MaximumTemperature
WHERE h.camera_id = histozoneMaxTemp.camera_id
ORDER BY h.Date ASC, h.Time ASC;
SELECT
a.*
FROM (
SELECT
x.id AS xid
, x.Date AS `Date`
, x.Time AS `Time`
, x.maximumtemperature AS maximumtemperature
, y.maximumtemperature AS previousmaximumtemperature
, x.maximumtemperature - y.maximumtemperature diff
,(x.maximumtemperature-y.maximumtemperature)/MAX(x.id) trend
FROM histoZoneMaxTempCamera{$cameraId} x
LEFT JOIN histoZoneMaxTempCamera{$cameraId} y
ON y.id = (x.id - 1)
GROUP BY x.id
) a
WHERE a.trend <> (
SELECT b.trend
FROM (
SELECT
x.id AS xid
,(x.maximumtemperature-y.maximumtemperature)/MAX(x.id) trend
FROM histoZoneMaxTempCamera{$cameraId} x
LEFT JOIN histoZoneMaxTempCamera{$cameraId} y
ON y.id = (x.id - 1)
GROUP BY x.id
) b
WHERE b.xid = a.xid - 1
) OR a.xid = 1
;
这很完美,即使在起始桌很大的情况下也非常快。