Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eon16-0005lE-J8 for pgsql-general@arkaria.postgresql.org; Thu, 22 Feb 2018 09:18:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eon14-0007lw-Gp for pgsql-general@arkaria.postgresql.org; Thu, 22 Feb 2018 09:18:14 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eon14-0007lm-5c for pgsql-general@lists.postgresql.org; Thu, 22 Feb 2018 09:18:14 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eon0z-0002fh-Ry for pgsql-general@postgresql.org; Thu, 22 Feb 2018 09:18:12 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 954E541125; Thu, 22 Feb 2018 10:18:06 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id AWMb6DvbXnmu; Thu, 22 Feb 2018 10:18:05 +0100 (CET) Message-ID: <1519291084.2587.4.camel@cybertec.at> Subject: Re: Selecting the most recent timestamptz From: Laurenz Albe To: Alexander Farber , pgsql-general Date: Thu, 22 Feb 2018 10:18:04 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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