How to use MAX_BY function in Apache IoTDB to return all timestamps when temperature is at maximum?
21:54 12 Apr 2026

In Apache IoTDB 2.0.6 table model, the MAX_BY function can return the timestamp when the data value is at its maximum. However, my requirement is to return all timestamps if multiple time points have the same maximum value. I tried this function, but it only returns one of them.

Here's my scenario:

  select * from sensor_data order by time
  +-----------------------------+---------+-----------+
  | time|sensor_id|temperature|
  +-----------------------------+---------+-----------+
  |2024-01-01T08:00:00.000+08:00| s001| 25.0|
  |2024-01-01T09:00:00.000+08:00| s002| 30.0|
  |2024-01-01T10:00:00.000+08:00| s003| 28.0|
  |2024-01-01T11:00:00.000+08:00| s004| 30.0|
  +-----------------------------+---------+-----------+

Executed SQL:

  SELECT max_by(time, temperature) FROM sensor_data

Returned Result:

  +-----------------------------+
  | _col0|
  +-----------------------------+
  |2024-01-01T11:00:00.000+08:00|
  +-----------------------------+

As you can see, the result only contains one of the timestamps corresponding to the maximum temperature value (30.0): 2024-01-01T11:00:00.000+08:00. However, in the original data, there's another timestamp with temperature 30.0: 2024-01-01T09:00:00.000+08:00.

Expected Result:

  +-----------------------------+
  | _col0|
  +-----------------------------+
  |2024-01-01T09:00:00.000+08:00|
  |2024-01-01T11:00:00.000+08:00|
  +-----------------------------+

How can I adjust the SQL statement to achieve my expectation and get all timestamps when the temperature is at its maximum value?

time-series apache-iotdb