From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Alexander Farber <alexander.farber@gmail.com>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Selecting the most recent timestamptz
Date: Thu, 22 Feb 2018 10:18:04 +0100
Message-ID: <1519291084.2587.4.camel@cybertec.at> (raw)
In-Reply-To: <CAADeyWhjXDF=XfsiJi8G3=sPKw5i+7_M7bQ84OQ+UB-rwKwp1w@mail.gmail.com>
References: <CAADeyWhjXDF=XfsiJi8G3=sPKw5i+7_M7bQ84OQ+UB-rwKwp1w@mail.gmail.com>
Alexander Farber wrote:
> Good morning, there are these 2 records in a table:
>
> # select m.played, m.mid, m.action, m.gid, m.uid from words_moves m where gid=10;
> played | mid | action | gid | uid
> -------------------------------+-----+--------+-----+-----
> 2018-02-19 14:42:08.46222+01 | 12 | play | 10 | 9
> 2018-02-20 15:06:01.430634+01 | 216 | expire | 10 | 11
> (2 rows)
>
> I try to get the record with the latest timestamp by adding a NOT EXISTS condition -
>
> # select m.played, m.mid, m.action, m.gid, m.uid from words_moves m where gid=10
> and not exists (select 1 from words_moves x where m.mid=x.mid AND x.played > m.played);
> played | mid | action | gid | uid
> -------------------------------+-----+--------+-----+-----
> 2018-02-19 14:42:08.46222+01 | 12 | play | 10 | 9
> 2018-02-20 15:06:01.430634+01 | 216 | expire | 10 | 11
> (2 rows)
>
> Why are still 2 records returned? I am probably overlooking something simple, sorry...
You are only checking if there is a later timestamp *for the same "mid"*.
Since the two rows have different "mid", they are not compared.
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: laurenz.albe@cybertec.at, alexander.farber@gmail.com
Subject: Re: Selecting the most recent timestamptz
In-Reply-To: <1519291084.2587.4.camel@cybertec.at>
References: <CAADeyWhjXDF=XfsiJi8G3=sPKw5i+7_M7bQ84OQ+UB-rwKwp1w@mail.gmail.com> <1519291084.2587.4.camel@cybertec.at>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox