Re: Fastest way / best practice to calculate "next birthdays"

Поиск
Список
Период
Сортировка
От Bosco Rama
Тема Re: Fastest way / best practice to calculate "next birthdays"
Дата
Msg-id 555E0488.1030908@boscorama.com
обсуждение исходный текст
Ответ на Re: Fastest way / best practice to calculate "next birthdays"  ("David G. Johnston" <david.g.johnston@gmail.com>)
Ответы Re: Fastest way / best practice to calculate "next birthdays"  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: Fastest way / best practice to calculate "next birthdays"  (Jim Nasby <Jim.Nasby@BlueTreble.com>)
Список pgsql-performance
On 05/20/15 20:22, David G. Johnston wrote:
> On Monday, May 18, 2015, er.tejaspatel88@gmail.com <
> er.tejaspatel88@gmail.com> wrote:
>
>> If I have to find upcoming birthdays in current week and the current week
>> fall into different months - how would you handle that?
>>
>
> Extract(week from timestamptz_column)
>
> ISO weeks are not affected by month boundaries but do start on Monday.

There is the year start/end boundary conditions to worry about there.

If the current week covers Dec28-Jan02 then week of year won't help for
a birthday on Jan01 or Jan02 if 'today' is in the Dec portion.  Ditto
for birthday in Dec portion when 'today' is in the Jan portion.

There is probably a better way to do it than what I'm showing here, but
here's an example:

with x as (
   select now() - (extract(dow from now()) || ' days')::interval as
weekstart
)
select to_char(x.weekstart, 'YYYY-MM-DD') as first_day,
        to_char(x.weekstart + '6 days', 'YYYY-MM-DD') as last_day
    from x;

You could probably make some of that into a function that accepts a
timestamptz and generates the two days.  Or even does the compare too.

HTH.

Bosco.


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

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: union all and filter / index scan -> seq scan
Следующее
От: "David G. Johnston"
Дата:
Сообщение: Re: Fastest way / best practice to calculate "next birthdays"