MIN/MAX functions for a record

Поиск
Список
Период
Сортировка
От Viliam Ďurina
Тема MIN/MAX functions for a record
Дата
Msg-id CAO=iB8L4WYSNxCJ8GURRjQsrXEQ2-zn3FiCsh2LMqvWq2WcONg@mail.gmail.com
обсуждение исходный текст
Ответы Re: MIN/MAX functions for a record  (Aleksander Alekseev <aleksander@timescale.com>)
Список pgsql-hackers
In my queries I often need to do MIN/MAX for tuples, for example:

  SELECT MAX(row(year, month)) 
  FROM (VALUES(2025, 1), (2024,2)) x(year, month);

This query throws:

    ERROR: function max(record) does not exist

In this case you can replace it with `MAX((year||'-'||month||'-1')::date)`. However in my case I have an event table with `event_time` and `text` columns, I'm grouping that table by some key and want to have the text for the newest event. I would do `MAX(ROW(event_time, text)).text`. Workarounds for this are clumsy, e.g. with a subquery with LIMIT 1.

The lack of this feature is kind of unexpected, because the `>` operator or `GREATEST` function are defined for records:

    SELECT 
        GREATEST((2025, 1), (2024, 2)), 
        (2025, 1) > (2024, 2)

Was this ever discussed or is there something preventing the implementation?

Viliam

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Ashutosh Bapat
Дата:
Сообщение: Re: Memory consumed by child SpecialJoinInfo in partitionwise join planning
Следующее
От: Sergey Prokhorenko
Дата:
Сообщение: Re: UUID v7