Re: Query question

From: Jeff Fletcher <jeff(dot)fletcher(at)gmail(dot)com>
To: Stanton Schmidt <sschmidt(at)rgllogistics(dot)com>
Cc: Martin Stöcker <martin(dot)stoecker(at)stb-datenservice(dot)de>, pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: Query question
Date: 2018-03-08 19:20:56
Message-ID: CABsD9ZNNiQxo=PQEW9xnXsWysGg6vntxC=TYxoh20E6DcG74yA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Partition by is your friend...

https://stackoverflow.com/questions/1124603/grouped-limit-in-postgresql-show-the-first-n-rows-for-each-group

On Thu, Mar 8, 2018 at 1:17 PM, Stanton Schmidt <sschmidt(at)rgllogistics(dot)com>
wrote:

> So far I have been unable to figure out how to do that.
>
> I tried:
> select a.equipment_id, b.log_date, b.log_time, b.event_desc
> from (select distinct equipment_id from log_table) a
> , (select equipment_id, log_date, log_time, event_desc from log_table
> order by log_date desc, log_time desc limit 5) b
> where a.equipment_id = b.equipment_id
>
> but all I end up with is 5 total records.
>
> Stanton
> ------------------------------
> *From: *"Martin Stöcker" <martin(dot)stoecker(at)stb-datenservice(dot)de>
> *To: *"pgsql-sql" <pgsql-sql(at)lists(dot)postgresql(dot)org>
> *Sent: *Thursday, March 8, 2018 1:07:30 PM
> *Subject: *Re: Query question
>
> My first idea is to select all equipments and lateral join them to the 5
> most recent events
>
> Regards Martin
>
> Am 08.03.2018 um 19:58 schrieb Stanton Schmidt:
>
> Hi,
> I am new to the list so feel free to let me know if I am out of line.
>
> 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
> )
>
> Thanks for your help.
>
> *stanton schmidt*
> *Database Administrator*
> direct. 920. <callto:920.884.1281>471.4495 cell 920.660.1828
> <(920)%20660-1828>
>
> RGL
> GO AHEAD. ASK WHAT IF.
> www.RGLlogistics.co m <http://www.rgllogistics.com/>
>
>
>
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2018-03-08 19:28:31 Re: Query question
Previous Message Stanton Schmidt 2018-03-08 19:17:07 Re: Query question