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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"er.tejaspatel88@xxxxxxxxx" <er.tejaspatel88@xxxxxxxxx> wrote:

> If I have to find upcoming birthdays in current week and the
> current week fall into different months - how would you handle
> that?

If you don't need to cross from December into January, I find the
easiest is:

SELECT * FROM person
  WHERE (EXTRACT(MONTH FROM dob), EXTRACT(DAY FROM dob))
          BETWEEN (6, 28) AND (7, 4);

That is logicically the same as:

SELECT * FROM person
  WHERE (EXTRACT(MONTH FROM dob) >= 6
    AND (EXTRACT(MONTH FROM dob) > 6
     OR (EXTRACT(DAY FROM dob) >= 28)))
    AND (EXTRACT(MONTH FROM dob) <= 7
    AND (EXTRACT(MONTH FROM dob) < 7
     OR (EXTRACT(DAY FROM dob) <= 4)));

That's the generalized case; with the months adjacent, this simpler
form is also equivalent:

SELECT * FROM person
  WHERE (EXTRACT(MONTH FROM dob) = 6
    AND EXTRACT(DAY FROM dob) >= 28)
    OR (EXTRACT(MONTH FROM dob) = 7
    AND EXTRACT(DAY FROM dob) <= 4);

The first query I showed is faster than either of the alternatives,
especially if there is an index on dob.

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-performance mailing list (pgsql-performance@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance




[Postgresql General]     [Postgresql PHP]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Yosemite]

  Powered by Linux