Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YzV7h-0007Xx-3l for pgsql-performance@arkaria.postgresql.org; Mon, 01 Jun 2015 19:11:45 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YzV7g-00014m-Kt for pgsql-performance@arkaria.postgresql.org; Mon, 01 Jun 2015 19:11:44 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YzV7f-00014c-Eq for pgsql-performance@postgresql.org; Mon, 01 Jun 2015 19:11:43 +0000 Received: from nm15-vm6.bullet.mail.ne1.yahoo.com ([98.138.91.108]) by makus.postgresql.org with esmtps (TLS1.0:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1YzV7a-00087N-NL for pgsql-performance@postgresql.org; Mon, 01 Jun 2015 19:11:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s2048; t=1433185897; bh=TB3pww01NGgP478V5ZBRGJSuxaLV2Kib3kwVhnaLa/Y=; h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject; b=kyusmodEl0+vQmVzZu8CZdbjKXL6o1zO4vA4FQsGDwEWRtV9wyFzugkeYsgZIoKGSrw+ax94n4PUT4r96VVrHmKPLrmUN/3kyWoydcAK+XdxWXPjiS7FMR3d6oa8p79FeQ4OGwU/08M2z/7ds4rQOkC7nEjRbjHGSL+5KMEE/STvgt6oM+cC/JjtCLa2YhOpKK+91pLrFDsA4nsngfkphayluRfC3nflfICcZWnhCMLMwDprEBYqqv4tOo6vIBXXgce1C8N5F+pjKOaK2Lwqep0K8gmRTQEiwqYozoTH645LYvqXRWHOXG2MdPrDSHfLMNrGvlbccNmrnOeb7rB4pw== Received: from [98.138.226.176] by nm15.bullet.mail.ne1.yahoo.com with NNFMP; 01 Jun 2015 19:11:37 -0000 Received: from [98.138.86.156] by tm11.bullet.mail.ne1.yahoo.com with NNFMP; 01 Jun 2015 19:11:37 -0000 Received: from [127.0.0.1] by omp1014.mail.ne1.yahoo.com with NNFMP; 01 Jun 2015 19:11:37 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 177114.74611.bm@omp1014.mail.ne1.yahoo.com X-YMail-OSG: AanTkJAVM1ll56ChDAkwruF23sB1uNcYrmIqVJ7P8Jni4mkvD9n96ZhLjXb4R2R kRmdZm1WunGilwIlBcXLLw26zNKinWddaM63r_tgtxeq2sRFZlSpjUDxKmej7uZ7o4rz5Vq0iMNN iPW53cvN6phsnS8UGtzV2ihMsb5OnX_tK7IejMdKLBBmbrZlvAa1wRqJT0F35pF8NkUf5qd8.osV qrZFj2wyhlgNGsWROmUjvQJkYRiMYAQYXXomhXMy_ZZsqU5TmTd0n8liJSfmxB6Hh5vAXwKwa5.F 10Hc5EJIN7vPq6VTukM3.EfLsGg0MAZwEDuYYh7ILMzaXThVske6p8_DmJuBN8kLsY_AowW5qZoX LhIGSvj9xWhAvI1vghV.fAfDS9yXLziMwb3jl8tsPXtUqXRJMRZT46RXaDYr_nkjR4KkTAnopaUM 93whwtOXkExwawu04TZraNEPY6tpi61HpfkBJhRFM8.a6mtrbJXwFEtgiWzXCR14K2zaU1Zp.YOf WQ9cUpAsTxy5JLlepYIuR41.r Received: by 98.138.105.203; Mon, 01 Jun 2015 19:11:36 +0000 Date: Mon, 1 Jun 2015 19:11:15 +0000 (UTC) From: Kevin Grittner Reply-To: Kevin Grittner To: "er.tejaspatel88@gmail.com" , "pgsql-performance@postgresql.org" Message-ID: <1217472852.2845919.1433185875147.JavaMail.yahoo@mail.yahoo.com> In-Reply-To: <1431941415888-5849705.post@n5.nabble.com> References: <1431941415888-5849705.post@n5.nabble.com> Subject: Re: Fastest way / best practice to calculate "next birthdays" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Length: 1140 X-Pg-Spam-Score: 0.4 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org "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