Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TAlZh-0002Ks-74 for pgsql-sql@postgresql.org; Sun, 09 Sep 2012 17:45:37 +0000 Received: from smtp103.prem.mail.ac4.yahoo.com ([76.13.13.42]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TAlZe-0002J3-9B for pgsql-sql@postgresql.org; Sun, 09 Sep 2012 17:45:36 +0000 Received: (qmail 15776 invoked from network); 9 Sep 2012 17:45:32 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=T0obA4LYuW1fnug3Yx1xw1LSjUM2XA8Vu3yeNkepDR36LQ3b+DOrzlWhYAMzayjXmIxF2CuURJIZI5R9A0ulm/eiSAOWQ+7yj/IFuMnYO4rOEdk4BvqpKfRyYZ0ZHD7n3OqvNovKC3YJTH8l/FEamB7xl1ML6TPX25L5F+k8DtM= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1347212732; bh=9BjKKaw3koR5H0aAmGEPhx+bO8b5IReYG8qBtYDgAMk=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=Y6xVQmsOMKO1cok6jMQc9isQwYQ3VY1no4L4KYrZFYyLTYKI9ZdxNIcxFb06T5t2S6mhC5NS131FitnYD/DxVFYWGMTTLb1awJY+Dkv3gXOl2/Ydw6/6jsnFEOndMwI3+I+N7T3hUUbrUkwgZDbxFjlqTMjvRaUMOWe1f6G8eyU= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: xqulEMUVM1mJZI7qVtt9Rnrxt2f.Cn00u9ASk5H_2TP8n2o Jme3KpMdVxQy8j6ZpnrpZ58alPrrTVFEKaixUEZHqOuQ.aGze7ycKoK.3z8k tVQSOFKbn_4uQhWKDy_wsmF307B4Pt5c01cgz.7R.2zUaVuuSBAyPC0fwbe1 emOElVBuBOq7ZvLdtp5yFLun98AgjyM.ohViqt_jb.Hl3dD_83PSKGPVMmkN 11rn1cohGAX1kpDdh8CQGPiv2Vmwv486LcbrQnzIS488pXzG98AsKeRWcfdG nMG74dIvMBwT.0OL3296aireV8LmpWnMd_VycPYSdLteQkHeSN3bJaWWjMw6 GjASA1Dwyp76YTjDaLV3lfzFQSNSh0y5z1Mm_aahTCMO1KySSoIyUPRZhtHi uKO7sT7BAhFdbT70_AsiiXd7uCdpR9MZHRLPO X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp103.prem.mail.ac4.yahoo.com with SMTP; 09 Sep 2012 10:45:31 -0700 PDT From: "David Johnston" To: "'JORGE MALDONADO'" , References: In-Reply-To: Subject: Re: Query with LIMIT clause Date: Sun, 9 Sep 2012 13:45:25 -0400 Message-ID: <019f01cd8eb2$e4f4be20$aede3a60$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQG7jKUwD0toHghyslOrE8cw9SAnyZemS0tg Content-Language: en-us X-Pg-Spam-Score: -1.0 (-) X-Archive-Number: 201209/16 X-Sequence-Number: 36818 From: pgsql-sql-owner@postgresql.org = [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of JORGE MALDONADO Sent: Sunday, September 09, 2012 1:26 PM To: pgsql-sql@postgresql.org Subject: [SQL] Query with LIMIT clause I have the following records that I get from a query, the fields are = date type in day/month/year format: -------------------------------------=20 Initial =A0 =A0 =A0 =A0 =A0 Final Date =A0 =A0 =A0 =A0 =A0 =A0Date ------------------------------------- 27/08/2012 =A0=A004/09/2012 29/08/2012 =A0=A009/09/2012 28/08/2012 =A0=A009/09/2012 30/08/2012 =A0=A009/09/2012 30/08/2012 =A0=A009/09/2012 27/08/2012 =A0=A009/09/2012 31/08/2012 =A0=A009/09/2012 28/08/2012 =A0=A010/09/2012 05/09/2012 =A0=A016/09/2012 As you can see, this result is ordered by Final Date. What I need is to = get the last 5 records only. If I use the LIMIT 5 clause I get the first 5. = I have tried an ORDEY BY DESC but the result is the same. I will very much appreciate your comments. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>. First, you should really order by both columns, not just "final date". = The ties on 9/9/12 are unordered. Second, you will indeed need to reverse the sort order and then take the first 5 records; figuring out and limiting on the last 5 isn't worth the effort. SELECT initial_date, final_date FROM date_source ORDER BY final_date DESC, initial_date DESC LIMIT 5 You can put the above into a sub-query and re-order if the final result = is needed in ascending order. If this doesn't seem to work you will want to provide the exact query/queries you are trying so that someone may spot what you are doing wrong. Dave