Select Top * From table

Hello experts

i am creating a latest news section on my clients website.
I want to show 5 news details  on each section. i have four sections in total thus needing 20 news title.

getting the top 5 is okay.

for the second section, i am using this query to get the details.
SELECT TOP 5 ID, NewsTitle, DocumentName, UploadDate FROM (SELECT TOP 5 ID, NewsTitle, DocumentName, UploadDate FROM (SELECT TOP 10 ID, NewsTitle, DocumentName, UploadDate FROM News ORDER BY UploadDate DESC) AS X ORDER BY UploadDate) AS Y ORDER BY UploadDate DESC

Now lets assume, that there is only 5 news details entered in the datable. this query will retrieve me the same 5 new details as with :
SELECT TOP 5 NewsTitle, DocumentName, UploadDate FROM _News ORDER BY UploadDate DESC

How can i avoid getting the same content on the both section. Initally when the website goes live there will be one or two news details entered. hence i need to cater for the above scenario as well.

Appreciate your help.
LVL 9
AsishRajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SoLostCommented:
You probably just need to include a WHERE statement in the section query.

... WHERE TOP_5_ID NOT IN (... Query that gets the global top 5 ...)
0
phhCommented:
Please try:

SELECT TOP 5 X.ID, X.NewsTitle, X.DocumentName, X.UploadDate FROM News as X
WHERE X.ID NOT IN (SELECT TOP 5 Y.ID FROM News AS Y ORDER BY Y.UploadDate ASC)
ORDER BY X.UploadDate ASC
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phhCommented:
and for the 3rd section it would be:

SELECT TOP 5 X.ID, X.NewsTitle, X.DocumentName, X.UploadDate FROM News as X
WHERE X.ID NOT IN (SELECT TOP 10 Y.ID FROM News AS Y ORDER BY Y.UploadDate ASC)
ORDER BY X.UploadDate ASC
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ThomasianCommented:
Are you using SQL Server 2005? If so, you could use the row_number function. I assumed that ID is the PK/unique in the table.
--First 5
SELECT NewsTitle, DocumentName, UploadDate
FROM
  (SELECT NewsTitle, DocumentName, UploadDate, ROW_NUMBER() OVER (ORDER BY UploadDate, ID) rn
     FROM _News 
  ) T
WHERE rn BETWEEN 1 AND 5

--Next 5
SELECT NewsTitle, DocumentName, UploadDate
FROM
  (SELECT NewsTitle, DocumentName, UploadDate, ROW_NUMBER() OVER (ORDER BY UploadDate, ID) rn
     FROM _News 
  ) T
WHERE rn BETWEEN 6 AND 10

Open in new window

0
ThomasianCommented:
If you need the ID or any other fields returned, just include it in both select statements.

i.e.
SELECT ID, News...
FROM
   (SELECT ID, News...
...
0
AsishRajAuthor Commented:
that worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.