Pregunta

I'm having problems with a custom order in a MySQL query. Any help would be appreciated.

So I have a table with the following data set:

id  SCADA  Date        Hour     Minute    Second   PlantNo  Key  Value 
 1   2924  2014-01-02  00:00:12 00:00:00  00:00:32    25    1300     0
 2   2924  2014-01-02  00:00:15 00:00:41  00:00:33     7    1300   500
 3   2924  2014-01-02  00:00:16 00:00:03  00:00:12    25    1300   500
 4   2924  2014-01-02  00:00:21 00:00:53  00:00:05    25    1300  1000
 5   2924  2014-01-02  00:00:21 00:00:53  00:00:05    26    1300  2060
 6   2924  2014-01-02  00:00:21 00:00:53  00:00:09     7    1300  1000
 7   2924  2014-01-03  00:00:07 00:00:42  00:00:06    25    1300  2060
 8   2924  2014-01-03  00:00:07 00:00:42  00:00:07     7    1300  2060
 9   2924  2014-01-03  00:00:12 00:00:00  00:00:03     5    1300    20
10   2924  2014-01-03  00:00:12 00:00:00  00:00:07     5    2501    18
11   2924  2014-01-04  00:00:11 00:00:52  00:00:56    16    1031     0
12   2924  2014-01-04  00:00:12 00:00:00  00:00:07     5    2501   8.5
13   2924  2014-01-04  00:00:13 00:00:51  00:00:05     4    1030     0
14   2924  2014-01-04  00:00:18 00:00:23  00:00:11     4    1030     1
15   2924  2014-01-06  00:00:16 00:00:08  00:00:36    26    1300  1500
16   2924  2014-01-07  00:00:17 00:00:11  00:00:00     5    1300    50
17   2924  2014-01-07  00:00:19 00:00:31  00:00:38     5    1030     0
18   2924  2014-01-07  00:00:21 00:00:00  00:00:53     5    1300   200
19   2924  2014-01-07  00:00:21 00:00:59  00:00:17     5    1300   500
20   2924  2014-01-08  00:00:08 00:00:28  00:00:53     5    1300  1000
21   2924  2014-01-08  00:00:08 00:00:56  00:00:33    26    1300   500
22   2924  2014-01-08  00:00:11 00:00:41  00:00:06    26    1300  1000
23   2924  2014-01-08  00:00:11 00:00:41  00:00:41     5    1300  1500

I need to SELECT only the last Value from each Key and each PlantNo. The last means the oldest date and time.

This is my code so far:

SELECT * FROM (
    SELECT SCADA, PlantNo, tblpartemp.Date, MAKETIME(tblpartemp.Hour,tblpartemp.Minute,tblpartemp.Second) AS Time, tblpartemp.Key, tblparameter.description, tblpartemp.Value
    FROM tblpartemp
    LEFT JOIN tblparameter ON tblpartemp.Key = tblparameter.id
    ORDER BY Date, Time DESC
) AS T1
GROUP BY T1.Key
¿Fue útil?

Solución

You can use the following query to achieve the result.

SELECT * FROM (SELECT * FROM plants ORDER BY Date DESC,Time DESC) AS t GROUP BY Plant

I assumed the table name as 'plants' and there were no other fields in the table.

Edited

Otros consejos

Once you've sorted out the flaws of your design, consider the following:

 DROP TABLE IF EXISTS my_table;

 CREATE TABLE my_table
 (id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,SCADA  INT NOT NULL
 ,Date  DATETIME NOT NULL
 ,PlantNo  INT NOT NULL
 ,my_key  INT NOT NULL
 ,my_value  INT NOT NULL
 );

 INSERT INTO my_table VALUES
 (1   ,2924  ,'2014-01-02 12:00:32'    ,25    ,1300 ,    0),
 (2   ,2924  ,'2014-01-02 15:41:33'    , 7    ,1300 ,  500),
 (3   ,2924  ,'2014-01-02 16:03:12'    ,25    ,1300 ,  500),
 (4   ,2924  ,'2014-01-02 21:53:05'    ,25    ,1300 , 1000),
 (5   ,2924  ,'2014-01-02 21:53:05'    ,26    ,1300 , 2060),
 (6   ,2924  ,'2014-01-02 21:53:09'    , 7    ,1300 , 1000),
 (7   ,2924  ,'2014-01-03 07:42:06'    ,25    ,1300 , 2060),
 (8   ,2924  ,'2014-01-03 07:42:07'    , 7    ,1300 , 2060),
 (9   ,2924  ,'2014-01-03 12:00:03'    , 5    ,1300 ,   20),
 (10   ,2924  ,'2014-01-03 12:00:07'    , 5    ,2501 ,   18),
 (11   ,2924  ,'2014-01-04 11:52:56'    ,16    ,1031 ,    0),
 (12   ,2924  ,'2014-01-04 12:00:07'    , 5    ,2501 ,  8.5),
 (13   ,2924  ,'2014-01-04 13:51:05'    , 4    ,1030 ,    0),
 (14   ,2924  ,'2014-01-04 18:23:11'    , 4    ,1030 ,    1),
 (15   ,2924  ,'2014-01-06 16:08:36'    ,26    ,1300 , 1500),
 (16   ,2924  ,'2014-01-07 17:11:00'    , 5    ,1300 ,   50),
 (17   ,2924  ,'2014-01-07 19:31:38'    , 5    ,1030 ,    0),
 (18   ,2924  ,'2014-01-07 21:00:53'    , 5    ,1300 ,  200),
 (19   ,2924  ,'2014-01-07 21:59:17'    , 5    ,1300 ,  500),
 (20   ,2924  ,'2014-01-08 08:28:53'    , 5    ,1300 , 1000),
 (21   ,2924  ,'2014-01-08 08:56:33'    ,26    ,1300 ,  500),
 (22   ,2924  ,'2014-01-08 11:41:06'    ,26    ,1300 , 1000),
 (23   ,2924  ,'2014-01-08 11:41:41'    , 5    ,1300 , 1500);

 SELECT x.*
  FROM my_table x
  JOIN
     ( SELECT plantno,my_key
            , MAX(date) max_date
         FROM my_table
        GROUP
           BY plantno
            , my_key
     ) y
    ON y.plantno = x.plantno
   AND y.my_key = x.my_key
   AND y.max_date = x.date
 ORDER
    BY plantno,my_key;
+----+-------+---------------------+---------+--------+----------+
| id | SCADA | Date                | PlantNo | my_key | my_value |
+----+-------+---------------------+---------+--------+----------+
| 14 |  2924 | 2014-01-04 18:23:11 |       4 |   1030 |        1 |
| 17 |  2924 | 2014-01-07 19:31:38 |       5 |   1030 |        0 |
| 23 |  2924 | 2014-01-08 11:41:41 |       5 |   1300 |     1500 |
| 12 |  2924 | 2014-01-04 12:00:07 |       5 |   2501 |        9 |
|  8 |  2924 | 2014-01-03 07:42:07 |       7 |   1300 |     2060 |
| 11 |  2924 | 2014-01-04 11:52:56 |      16 |   1031 |        0 |
|  7 |  2924 | 2014-01-03 07:42:06 |      25 |   1300 |     2060 |
| 22 |  2924 | 2014-01-08 11:41:06 |      26 |   1300 |     1000 |
+----+-------+---------------------+---------+--------+----------+

The hack equivalent to this query is as follows:

SELECT *
  FROM
     ( SELECT *
         FROM my_table
        ORDER
           BY Date DESC
     ) t
 GROUP
    BY plantno
     , my_key;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top