문제

I got a problem with a mySql query and max() function. If I do :

Select * from Data group by experiment having min(timestamp)

This query return what I want, and correct value. I got this :

+----------+---------+----------+---------------------+----------------+------------+
| id       | mote_id | label_id | timestamp           | value          | experiment |
+----------+---------+----------+---------------------+----------------+------------+
|  3768806 |      10 |       30 | 2014-04-22 14:37:07 |              0 |         13 |
| 10989209 |      12 |       22 | 2014-04-25 10:44:03 | 2.532958984375 |         15 |
| 11943537 |       6 |       19 | 2014-05-05 17:20:15 |           1228 |         16 |
| 12042549 |      16 |       26 | 2014-05-06 10:48:59 |          22.86 |         17 |
| 12176642 |      15 |       23 | 2014-05-07 15:19:35 |              0 |         18 |
| 12195344 |      10 |        6 | 2014-05-07 15:27:23 |           3460 |         19 |
| 12222470 |      15 |        8 | 2014-05-07 15:38:38 |              1 |         21 |
| 12343934 |      10 |       19 | 2014-05-12 10:35:42 |            742 |         23 |
+----------+---------+----------+---------------------+----------------+------------+

But, if i do :

Select * from Data group by experiment having max(timestamp)

This query return wrong values... like this :

+----------+---------+----------+---------------------+----------------+------------+
| id       | mote_id | label_id | timestamp           | value          | experiment |
+----------+---------+----------+---------------------+----------------+------------+
|  3768806 |      10 |       30 | 2014-04-22 14:37:07 |              0 |         13 |
| 10989209 |      12 |       22 | 2014-04-25 10:44:03 | 2.532958984375 |         15 |
| 11943537 |       6 |       19 | 2014-05-05 17:20:15 |           1228 |         16 |
| 12042549 |      16 |       26 | 2014-05-06 10:48:59 |          22.86 |         17 |
| 12176642 |      15 |       23 | 2014-05-07 15:19:35 |              0 |         18 |
| 12195344 |      10 |        6 | 2014-05-07 15:27:23 |           3460 |         19 |
| 12222470 |      15 |        8 | 2014-05-07 15:38:38 |              1 |         21 |
| 12343934 |      10 |       19 | 2014-05-12 10:35:42 |            742 |         23 |
+----------+---------+----------+---------------------+----------------+------------+

In the first query, if I replace min(timestamp) by timestamp=min(timestamp), it works, but in the second, "timestamp=max(timestamp)" return nothing

Finally, Select experiment,max(timestamp) return correct values.

mysql> select *,max(timestamp) from Data group by experiment;

+----------+---------+----------+---------------------+----------------+------------+---------------------+
| id       | mote_id | label_id | timestamp           | value          | experiment | max(timestamp)      |
+----------+---------+----------+---------------------+----------------+------------+---------------------+
|  3768806 |      10 |       30 | 2014-04-22 14:37:07 |              0 |         13 | 2014-04-24 16:03:29 |
| 10989209 |      12 |       22 | 2014-04-25 10:44:03 | 2.532958984375 |         15 | 2014-05-05 10:34:35 |
| 11943537 |       6 |       19 | 2014-05-05 17:20:15 |           1228 |         16 | 2014-05-06 10:35:15 |
| 12042549 |      16 |       26 | 2014-05-06 10:48:59 |          22.86 |         17 | 2014-05-07 15:19:33 |
| 12176642 |      15 |       23 | 2014-05-07 15:19:35 |              0 |         18 | 2014-05-07 15:27:23 |
| 12195344 |      10 |        6 | 2014-05-07 15:27:23 |           3460 |         19 | 2014-05-07 15:38:01 |
| 12222470 |      15 |        8 | 2014-05-07 15:38:38 |              1 |         21 | 2014-05-07 16:30:38 |
| 12343934 |      10 |       19 | 2014-05-12 10:35:42 |            742 |         23 | 2014-05-14 09:25:44 |
+----------+---------+----------+---------------------+----------------+------------+---------------------+

I know I can make a subquery to solve my probleme, but the tables contains thousands rows, and this solution is too long...

Ps : I can't use Select*, max(timestamp) even if it works because the query is run by EJB in JEE.

도움이 되었습니까?

해결책

You select not determined values grouped by field experiment. No one can give you a guarantee that non-agregated fields would correspond to MIN or MAX values of some aggregated field.

You HAVE TO use sub-query or self-join to get the right records.

See more here: http://dev.mysql.com/doc/refman/5.6/en/example-maximum-column-group-row.html

다른 팁

The HAVING clause expects a boolean expression. In other DBMS your code sample would trigger an error. In MySQL, you'll get the expression cast to boolean:

  • Zero → false
  • Non-zero → true

And since your expression is constant for the whole set, it won't filter out partial rows.

As about this:

HAVING timestamp = max(timestamp)

The HAVING clause evaluates after WHERE and GROUP BY. At that point, using individual row values of the timestamp column doesn't make any sense. As usual, MySQL allows that but you must take into account that:

In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the HAVING clause that are not named in the GROUP BY clause. A MySQL extension permits references to such columns to simplify calculations. This extension assumes that the nongrouped columns will have the same group-wise values. Otherwise, the result is indeterminate.

In other words, your results are arbitrary (not even random).

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