From: Kevin Grittner <kgrittn@ymail.com>
To: er.tejaspatel88@gmail.com <er.tejaspatel88@gmail.com>
To: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: Fastest way / best practice to calculate "next birthdays"
Date: Mon, 1 Jun 2015 19:11:15 +0000 (UTC)
Message-ID: <1217472852.2845919.1433185875147.JavaMail.yahoo@mail.yahoo.com> (raw)
In-Reply-To: <1431941415888-5849705.post@n5.nabble.com>
References: <1431941415888-5849705.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
"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?
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@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
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 using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: kgrittn@ymail.com, er.tejaspatel88@gmail.com
Subject: Re: Fastest way / best practice to calculate "next birthdays"
In-Reply-To: <1217472852.2845919.1433185875147.JavaMail.yahoo@mail.yahoo.com>
* 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