pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: whereverlei@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15551: Date/Time comparison not correct when the comparison is inside join clause and involves "+" or "-"
Date: Fri, 14 Dec 2018 11:32:31 +0000
Message-ID: <87o99oz6w8.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <15551-45aab520cab43cc9@postgresql.org>
References: <15551-45aab520cab43cc9@postgresql.org>

>>>>> "PG" == PG Bug reporting form <noreply@postgresql.org> writes:

 PG> Note: the fourth quarter (*-12-31) of all years are missing.

So the problem here is that you're trying to identify quarters by the
_last_ date, not by the _first_ date.

If you add 3 months to Sept 30th, you get Dec 30th, NOT Dec 31st.
It so happens that adding 3 months to Mar 31st gives Jun 30th, and
adding 3 months to Jun 30th gives Sep 30th, and adding 3 months to
Dec 31st gives Mar 31st, so it's only the one case that fails here.

But if you identify the quarter by its _first_ date, you have no
problem.

date_trunc('quarter', somedate::timestamp) can help with this, but
remember to cast the date to timestamp (without time zone) in the call,
otherwise you'll get incorrect results due to timezone issues.

 PG> The only difference among these query lies in the join clause
 PG> involving date / time comparison having date / time operators being
 PG> used:

 PG> 1st: ct1."time" = ct2."time" + interval '3 months'
 PG> 2nd: ct2."time" = ct1."time" - interval '3 months'
 PG> 3rd: ct1."time" = ct2."time" + interval '3 months' or ct2."time" =
 PG> ct1."time" - interval '3 months'

 PG> It seems to me all the 3 conditions are logically same and should
 PG> have the same result.

Well, in the presence of months of variable lengths, they clearly are
not equivalent and cannot be. So no bug here.

-- 
Andrew (irc:RhodiumToad)




view thread (2+ messages)

Message-ID: <87o99oz6w8.fsf@news-spur.riddles.org.uk>
Permalink:  ../87o99oz6w8.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87o99oz6w8.fsf@news-spur.riddles.org.uk

 · 

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 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-bugs@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, whereverlei@gmail.com
  Subject: Re: BUG #15551: Date/Time comparison not correct when the comparison is inside join clause and involves "+" or "-"
  In-Reply-To: <87o99oz6w8.fsf@news-spur.riddles.org.uk>
  References: <15551-45aab520cab43cc9@postgresql.org> <87o99oz6w8.fsf@news-spur.riddles.org.uk>

* 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