Re: Fetching last n records from Posgresql

From: Moreno Andreo <moreno(dot)andreo(at)evolu-s(dot)it>
To: "Deole, Pushkar (Pushkar)" <pdeole(at)avaya(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Fetching last n records from Posgresql
Date: 2016-03-30 09:42:15
Message-ID: 56FB9F77.4010106@evolu-s.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Il 30/03/2016 11:36, Deole, Pushkar
(Pushkar) ha scritto:<br>
</div>
<blockquote
cite="mid:78A4D6BFBAA5BA49A5E94DA00A6A76E309864486(at)AZ-FFEXMB04(dot)global(dot)avaya(dot)com"
type="cite">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered
medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";
color:black;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
pre
{mso-style-priority:99;
mso-style-link:"HTML Preformatted Char";
margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";
color:black;}
span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:Consolas;
color:black;}
span.EmailStyle19
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:#1F497D;}
span.EmailStyle20
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">I
am sorry I didn’t clarify my requirement properly.. I want
the ‘n’ oldest records, however, they should sorted with the
recent record first and I want this to happen in the query
itself so I don’t have to care about sorting through the
application..<o:p></o:p></span></p>
<p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #B5C4DF
1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span
style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;;color:windowtext">From:</span></b><span
style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;;color:windowtext">
<a class="moz-txt-link-abbreviated" href="mailto:pgsql-general-owner(at)postgresql(dot)org">pgsql-general-owner(at)postgresql(dot)org</a>
[<a class="moz-txt-link-freetext" href="mailto:pgsql-general-owner(at)postgresql(dot)org">mailto:pgsql-general-owner(at)postgresql(dot)org</a>]
<b>On Behalf Of </b>Moreno Andreo<br>
<b>Sent:</b> Wednesday, March 30, 2016 3:03 PM<br>
<b>To:</b> <a class="moz-txt-link-abbreviated" href="mailto:pgsql-general(at)postgresql(dot)org">pgsql-general(at)postgresql(dot)org</a><br>
<b>Subject:</b> Re: [GENERAL] Fetching last n records
from Posgresql<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Il 30/03/2016 11:19, Deole, Pushkar
(Pushkar) ha scritto:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal">select * from t order by record_date desc
limit 5;<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">this will return the recent 5 records..
what I want is the oldest 5 records (in last 30 days)<o:p></o:p></p>
</blockquote>
<p class="MsoNormal" style="margin-bottom:12.0pt">so remove
"desc", in order to have ascending ordering, thus first 5
records are the five oldest:<br>
<br>
select * from t order by record_date limit 5<br>
<br>
Cheers<br>
Moreno.-<br>
<br>
<o:p></o:p></p>
</div>
</blockquote>
select * from (select * from t where record_date &gt;=current_date()
- '30 days' order by record_date limit 5) order by record_date desc<br>
<br>
Not tested, but should work... <br>
<br>
Cheers<br>
Moreno.-<br>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 4.9 KB

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sándor Daku 2016-03-30 09:45:53 Re: Fetching last n records from Posgresql
Previous Message Deole, Pushkar (Pushkar) 2016-03-30 09:36:47 Re: Fetching last n records from Posgresql