agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-sql@postgresql.org
Subject: Re: Query question
Date: Thu, 8 Mar 2018 20:48:35 +0100
Message-ID: <p7s3uj$dg0$1@blaine.gmane.org> (raw)
In-Reply-To: <415803152.48510563.1520535505737.JavaMail.zimbra@rglholdings.com>
References: <415803152.48510563.1520535505737.JavaMail.zimbra@rglholdings.com>
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?
view thread (15+ messages) latest in thread
Message-ID: <p7s3uj$dg0$1@blaine.gmane.org>
Permalink: ../p7s3uj$dg0$1@blaine.gmane.org/
Also on: postgresql.org/message-id/p7s3uj$dg0$1@blaine.gmane.org
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: spam_eater@gmx.net
Subject: Re: Query question
In-Reply-To: <p7s3uj$dg0$1@blaine.gmane.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox