Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

limiting the # of rows in a query

Posted on 2002-04-26
7
157 Views
Last Modified: 2013-12-24
Hello and thanks in advance for looking at this.

I'd like to select the 10 most recent items added to my database table. I'd like to pick these 10 items by the date they were added to the db.

Each of these items has a title AND a category. I'd like to get the 10 results and then group them by their category names.

Any ideas as to how I can go about accomplishing this?

Peter Edd
0
Comment
Question by:peteredd
7 Comments
 

Expert Comment

by:dnacerny
ID: 6972516
1) Make sure you have a 'CREATED_ON' field in your table.  Populate this field when inserting with the sysdate (oracle) or Now() (access).  These also include time stamps so it will contain the date and time of the entry.

2) Perform a sub-query to order the data by your 'CREATED_ON' field - this will give you the most recent entries first.  With the outer query return the sub set of 10 rows and order it by the field you desire.  Here is an example:

select title, category
  from (
     select *
       from table
      order by created_on desc
        )
 where rownum < 11
order by title ;

Hope this helps!!
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6973245
Try this

select top 10 *
from your_table
order by date_create desc


hongjun
0
 

Author Comment

by:peteredd
ID: 6973407
Actually, the answer was a combination of the two replies received. I will look to split the points between you two and thank you both very much.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:peteredd
ID: 6973412
I am using SQL, not Oracle, here's what the query ended up looking like and it works great.

select title, category
 from (
    select top 10 *
    from table
    order by created_on desc
       )
order by category;

thanks again
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6974150
Points reduced for split.  Now you can accept an experts comment as an answer.  After that, make another question in this topic area for the other expert.

Computer101
E-E Moderator
0
 
LVL 33

Accepted Solution

by:
hongjun earned 100 total points
ID: 6974340
peterdd, accept my comment here and then post another question with the same points (100) in this Topic Area. Information of the thread will be like this
Subject: points for dnacerny
Points: 100
Body of Question: thanks for your help in http://www.experts-exchange.com/jsp/qShow.jsp?qid=20294076



hongjun
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6975215
-1 userid problem fixed, points moved to hongjun.
Moondancer - EE Moderator
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question