ClickHouse: How to get argMax for array of tuples

Viewed 290

The task is to get the tuple with the maximum first item. Is there any better way than the following?

select arrayMax(u.a.1) first_item_max_in_array, 
    indexOf(u.a.1,first_item_max_in_array) index_in_array, 
    u.a[index_in_array].2 arg_max_second_item_first_item 
from (
    select [(1,10),(2,20),(3,30)] a
) u
1 Answers

Consider using Array-combinator:

WITH [(1, 10), (2, 20), (3, 30)] AS arr
SELECT argMaxArray(arr.2, arr.1) AS result

/*
┌─result─┐
│     30 │
└────────┘
*/
Related