Re: Inconsistent results in timestamp/interval comparison

Поиск
Список
Период
Сортировка
От Francisco Olarte
Тема Re: Inconsistent results in timestamp/interval comparison
Дата
Msg-id CA+bJJbwvj1_e+2fVPf-kD4Q1pfsMqYUP47++p2OzVGmVx_zy0Q@mail.gmail.com
обсуждение исходный текст
Ответ на Inconsistent results in timestamp/interval comparison  (albrecht.dress@posteo.de)
Ответы Re: Inconsistent results in timestamp/interval comparison
Re: Inconsistent results in timestamp/interval comparison
Список pgsql-general
On Mon, 4 Mar 2024 at 13:10, <albrecht.dress@posteo.de> wrote:
> According to the documentation, Table 9.31, IMHO both comparisons should
> produce the same results, as

> timestamp - timestamp → interval
> timestamp + interval → timestamp
Your problem may be due to interval comparison.

Intervals are composed of months, days and seconds, as not every month
has 30 days and not every day has 86400 seconds, so to compare them
you have to normalize them somehow, which can lead to bizarre results.

=> select '2 years'::interval > '1 year 362 days'::interval;
 ?column?
----------
 f
(1 row)

=> select '2 years'::interval > '1 year 359 days'::interval;
 ?column?
----------
 t
(1 row)

=> select '2 years'::interval > '1 year 360 days'::interval;
 ?column?
----------
 f
(1 row)

=> select '2 years'::interval = '1 year 360 days'::interval;
 ?column?
----------
 t
(1 row)

If you want to do point in time arithmetic, you will be better of by
extracting epoch from your timestamps and substracting that. Intervals
are more for calendar arithmetic on the type "set me a date two
months, three days and four hours from the last".

Francisco Olarte.



В списке pgsql-general по дате отправления:

Предыдущее
От: Michał Kłeczek
Дата:
Сообщение: Re: postgres_fdw aggregate pushdown for group by with expressions
Следующее
От: Alban Hertroys
Дата:
Сообщение: Re: Inconsistent results in timestamp/interval comparison