Solved

limiting the # of rows in a query

Posted on 2002-04-26
7
130 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now