문제

I have a very large table where I have temperatures getting logged every 1 mins, what I would like to query is a trend; something like a percentage increase or percentage decrease per selected period ( hour or 15mins; depending on the query)

my table looks (example) like the following

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

I believe a trend can be calculated as follow (as per link), but correct me if you have a better way; take the difference between each row then add then up and divide by count. so for the table above we get

Diff
+0.25
+0.10
+0.20
-0.10
-0.20
-0.25
-0.15
-0.20
+0.20
+0.15
+0.35

The trend per minute for last 11 minutes is sum of diff/11. which gives 0.063C per minute for last 11minutes.

Can someone please help me get percentage trend per hour for last 3 hours. and trend per minute for 1 hour?

도움이 되었습니까?

해결책

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

Incidentally, if you're interested in getting average results per hour, you could do something like this...

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');

다른 팁

I know the subject is old but if i can share with you my experience. maybe this can be usefull for the next people :)

I've a very large table with temperature of my all devices (100+) and all my devices push every 5 secondes the temperatures (one device has 6 zones of vision, and i can get the temperature of each zone).

So the table is very big. For me the previous response is not efficient with lot of data. See what i do:

This is the schema of my large table:

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`),
);

Like you can see, I've the Date and Time for each row.

  1. Create a temporary table for each device with the MEMORY Engine in my PHP code. I restrict the tmp table in a duration (date and 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;
  1. And the last step, is to get the data, the trend, etc ... Me, I only wanted the fluctuating temperature points. It's not interesting to have a graph with a lot of points at the same level.
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
;

This works perfectly, and is super fast even with a very large starting table.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top