Re: Query question

Поиск
Список
Период
Сортировка
От Thomas Kellerer
Тема Re: Query question
Дата
Msg-id p7s3uj$dg0$1@blaine.gmane.org
обсуждение исходный текст
Ответ на Query question  (Stanton Schmidt <sschmidt@rgllogistics.com>)
Список pgsql-sql
Stanton Schmidt schrieb am 08.03.2018 um 19:58:
> My question is:
> I have a table that has log events for pieces of equipment.  For each piece of equipment this table may contain 1 or
more(hundreds potentially).
 
> I need to write a query that will return only the last 5 log events for each (and every) piece of equipment.
> 
> log_table (
> equipment_id character(30),
> log_date date,
> log_time time,
> event_desc text
> )

Queries like that are typically solved using window functions:

     select *
     from (
       select equipment_id,
              log_date,
              log_time,
              event_desc,
              row_number() over (partition by equipment_id order by log_date desc, log_time desc) as rn
       from log_table
     ) t
     where rn <= 5;


Unrelated, but: why aren't you storing "log_date_time" in a single timestamp?





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

Предыдущее
От: "David G. Johnston"
Дата:
Сообщение: Re: Query question
Следующее
От: Stanton Schmidt
Дата:
Сообщение: Re: Query question