Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gXlhv-0006XG-3a for pgsql-bugs@arkaria.postgresql.org; Fri, 14 Dec 2018 11:32:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gXlht-0000t3-QC for pgsql-bugs@arkaria.postgresql.org; Fri, 14 Dec 2018 11:32:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gXlht-0000sv-KL for pgsql-bugs@lists.postgresql.org; Fri, 14 Dec 2018 11:32:37 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gXlhr-0006kw-C7 for pgsql-bugs@lists.postgresql.org; Fri, 14 Dec 2018 11:32:37 +0000 Received: from [192.168.127.1] (port=57935 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gXlho-0000SS-VO; Fri, 14 Dec 2018 11:32:32 +0000 Received: from [127.0.0.1] (port=47299 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gXlho-0003e7-3d; Fri, 14 Dec 2018 11:32:32 +0000 From: Andrew Gierth 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 "-" In-Reply-To: <15551-45aab520cab43cc9@postgresql.org> (PG Bug reporting form's message of "Fri, 14 Dec 2018 09:14:44 +0000") Message-ID: <87o99oz6w8.fsf@news-spur.riddles.org.uk> References: <15551-45aab520cab43cc9@postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 14 Dec 2018 11:32:31 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "PG" == PG Bug reporting form 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)