Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 44C79475FBC for ; Fri, 21 Jun 2002 04:38:25 -0400 (EDT) Received: from mailout06.sul.t-online.com (mailout06.sul.t-online.com [194.25.134.19]) by postgresql.org (Postfix) with ESMTP id 74B3F475D81 for ; Fri, 21 Jun 2002 04:31:05 -0400 (EDT) Received: from fwd10.sul.t-online.de by mailout06.sul.t-online.com with smtp id 17LJov-0007Xf-01; Fri, 21 Jun 2002 10:31:05 +0200 Received: from avocado.shifted-bytes.de (520056191158-0001@[217.5.59.69]) by fmrl10.sul.t-online.com with esmtp id 17LJos-29MAXwC; Fri, 21 Jun 2002 10:31:02 +0200 Received: from shifted-bytes.de (orange.shifted-bytes.de [10.0.0.14]) by avocado.shifted-bytes.de (8.12.2/8.12.2) with ESMTP id g5L8UumI010596; Fri, 21 Jun 2002 10:31:01 +0200 (CEST) Message-ID: <3D12E43E.3090307@shifted-bytes.de> Date: Fri, 21 Jun 2002 10:30:54 +0200 From: Michael Agbaglo User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:0.9.4.1) Gecko/20020508 Netscape6/6.2.3 X-Accept-Language: en-us MIME-Version: 1.0 To: josh@agliodbs.com Cc: pgsql-sql@postgresql.org Subject: Re: how to sort a birthday list ? References: <3D122508.70104@shifted-bytes.de> <200206201703.52435.josh@agliodbs.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Sender: 520056191158-0001@t-dialin.net X-Archive-Number: 200206/287 X-Sequence-Number: 8252 Dear Josh, if you sort by AGE then the order will be young -> old or vice versa. I'd like to have the list sorted as interval birthDAY, birthMONTH and DAY from NOW() and MONTH from NOW(). example: 22.06.64 Person-1 26.06.50 Person-2 01.08.69 Person-3 02.08.71 Person-4 ... of course you could sort by DOY but then you'll have a problem w/ the next year: if it's let's say december and you select the list for the next 60 days, persons having birthday in december will appear after persons having birthday in january. I tried to use CASE WHEN... THEN in ORDER BY but it doesn't seem to work (syntax error at '') M. Josh Berkus wrote: > Michael, > > >>SELECT * >>FROM Persons >>WHERE EXTRACT( YEAR FROM AGE(dateofbirth) ) < EXTRACT( YEAR FROM AGE( >>CURRENT_DATE+60, dateofbirth ) ) >> >>... but how do I sort the list ? >> > > Easy: > > SELECT person_name, person_department, EXTRACT( YEAR FROM AGE(dateofbirth) ) > as their_age > FROM Persons > WHERE EXTRACT( YEAR FROM AGE(dateofbirth) ) < EXTRACT( YEAR FROM AGE( > CURRENT_DATE+60, dateofbirth ) ) > ORDER BY their_age, person_name > > As an example. > > > > > >