# MAX\_BY function

**URL:** <https://www.singlestore.com/forum/t/max-by-function/2527>\
**Category:** Feedback\
**Created:** [November 12, 2020, 11:55am UTC](https://www.singlestore.com/forum/t/max-by-function/2527 "2020-11-12T11:55:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keneck](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@Keneck](https://www.singlestore.com/forum/u/Keneck)\
**Post date:** [November 12, 2020, 11:55am UTC](https://www.singlestore.com/forum/t/max-by-function/2527/1 "2020-11-12T11:55:08Z")

</div>

Hi,

It would be great if SingleStore had the `MAX_BY`/`MIN_BY` aggregation functions, as it’s a very common function used in OLAP.

Any suggestions around the best performant workaround to achieve what `MAX_BY`/`MIN_BY` do with the current functions supported by SingleStore, in a Columnstore table?

Thanks!

---

<div class="post-metadata">

**Author:** ![rob](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.singlestore.com/forum/u/rob)\
**Post date:** [November 19, 2020, 5:22pm UTC](https://www.singlestore.com/forum/t/max-by-function/2527/2 "2020-11-19T17:22:41Z")

</div>

Hi, We currently have the FIRST and LAST builtin aggregate functions: [SingleStoreDB Cloud · SingleStore Documentation](https://docs.singlestore.com/v7.1/reference/sql-reference/time-series-functions/first/). Would this work for your use case?

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [December 3, 2020, 8:37pm UTC](https://www.singlestore.com/forum/t/max-by-function/2527/3 "2020-12-03T20:37:16Z")

</div>

@Keneck I think FIRST and LAST can do what you need as long as your second (ordering) argument is a datetime or timestamp. Will that work for you?

See [SingleStoreDB Cloud · SingleStore Documentation](https://docs.singlestore.com/v7.1/reference/sql-reference/time-series-functions/first/)

Can you share an example of MAX\_BY and MIN\_BY used in one of your queries?

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [December 21, 2020, 11:35pm UTC](https://www.singlestore.com/forum/t/max-by-function/2527/4 "2020-12-21T23:35:28Z")

</div>

HI @Keneck, do you could answer my question just above?

---

<div class="post-metadata">

**Author:** ![Keneck](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/k/5daacb/32.png) [@Keneck](https://www.singlestore.com/forum/u/Keneck)\
**Post date:** [December 27, 2020, 4:16pm UTC](https://www.singlestore.com/forum/t/max-by-function/2527/5 "2020-12-27T16:16:33Z")

</div>

@rob @hanson Yes, whenever the ordering argument is a `datetime` or a `timestamp`, the `FIRST` and `LAST` functions work great. However, if you have a table like the one below, that stores temperature readings of multiple devices, what is the most efficient query that could be used to obtain, for each device, the `dt` when the maximum and minimum temperatures were recorded?

```
CREATE TABLE `temperatures` (
  `device_id` int,
  `dt` datetime,
  `temperature` float
);

```

Thanks!

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [January 4, 2021, 7:32pm UTC](https://www.singlestore.com/forum/t/max-by-function/2527/6 "2021-01-04T19:32:01Z")

</div>

Try something like

```auto
CREATE TABLE `temperatures` (
  `device_id` int,
  `dt` datetime series timestamp,
  `temperature` float
);

select device_id, first(temperature), last(temperature), 
  min(temperature), max(temperature), 
  min(dt) as first_dt, max(dt) as last_dt
from temperatures
group by device_id;

```

Using min and max on dt can give you the first and last dt values for the device\_id you are grouping by.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [January 4, 2021, 7:39pm UTC](https://www.singlestore.com/forum/t/max-by-function/2527/7 "2021-01-04T19:39:51Z")

</div>

Oh, I see you are asking a different question. You could do it with a user-defined aggregate that keeps around the time when the min and max temperatures were recorded and includes that in the output.

Alternatively, you could find your min and max temps for each device in a CTE, and then join on temperature to get the corresponding dt value.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [January 4, 2021, 11:43pm UTC](https://www.singlestore.com/forum/t/max-by-function/2527/8 "2021-01-04T23:43:29Z")

</div>

And I see [this description of MAX\_BY](https://docs.data.world/documentation/sql/reference/aggregations/max_by.html#:~:text=MAX_BY%20takes%20two%20arguments%20and,numeric%20and%20non%2Dnumeric%20data.) on the web.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [January 5, 2021, 12:53am UTC](https://www.singlestore.com/forum/t/max-by-function/2527/9 "2021-01-05T00:53:33Z")

</div>

I opened an internal feature request for MIN\_BY and MAX\_BY to track this.

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [January 5, 2021, 12:55am UTC](https://www.singlestore.com/forum/t/max-by-function/2527/10 "2021-01-05T00:55:11Z")

</div>

Here’s how you could do it with a user-defined aggregate.

```auto
DELIMITER //
CREATE FUNCTION max_by_init() RETURNS 
    RECORD(dt DATETIME(6), val DOUBLE) AS
  BEGIN
    RETURN ROW(NULL, NULL);
  END //
DELIMITER ;
DELIMITER //
CREATE FUNCTION max_by_iter(
    state RECORD(dt DATETIME(6), val DOUBLE),
    dt datetime(6),
    val DOUBLE) RETURNS RECORD(dt DATETIME(6), val DOUBLE)
  AS
  BEGIN
    RETURN ROW(
       if(val > state.val, dt, state.dt),
       if(val > state.val, val, state.val));
  END //
DELIMITER ;
DELIMITER //
CREATE FUNCTION max_by_merge(state1 RECORD(dt DATETIME(6), val DOUBLE), 
                             state2 RECORD(dt DATETIME(6), val DOUBLE)) 
    RETURNS RECORD(dt DATETIME(6), val DOUBLE) AS
  BEGIN
    RETURN row(if(state1.val > state2.val, state1.dt, state2.dt),
               if(state1.val > state2.val, state1.val, state2.val));
  END //
DELIMITER ;
DELIMITER //
CREATE FUNCTION max_by_terminate(state RECORD(dt DATETIME(6), val DOUBLE)) 
    RETURNS DATETIME(6) AS
  BEGIN
    RETURN state.dt;
  END //
DELIMITER ;

CREATE AGGREGATE max_by(DATETIME(6), DOUBLE) RETURNS DATETIME(6)
  WITH STATE RECORD(dt DATETIME(6), val DOUBLE)
  INITIALIZE WITH max_by_init
  ITERATE WITH max_by_iter
  MERGE WITH max_by_merge
  TERMINATE WITH max_by_terminate;

-- test
create table ts(d datetime(6), v double);

insert ts values(now(6), 1);
insert ts values(now(6), 2);
insert ts values(now(6), 3);
insert ts values(now(6), 2);
insert ts values(now(6), 1);

select * from ts order by d;

+----------------------------+------+
| d | v |
+----------------------------+------+
| 2021-01-04 16:39:52.411670 | 1 |
| 2021-01-04 16:39:57.351114 | 2 |
| 2021-01-04 16:40:04.794208 | 3 |
| 2021-01-04 16:40:13.367547 | 2 |
| 2021-01-04 16:40:19.969650 | 1 |
+----------------------------+------+

select max(v), max_by(d, v)
from ts;

+--------+----------------------------+
| max(v) | max_by(d, v) |
+--------+----------------------------+
| 3 | 2021-01-04 16:40:04.794208 |
+--------+----------------------------+

```
