Solved

Select TOP 3 items based on date

Posted on 2008-10-08
7
171 Views
Last Modified: 2010-03-19
My client has a database table containing news items for their website.

I need to run a select statement that grabs the most recent 3 news items that have been posted within the last 3 months.  If 3 items have been posted in the 3 month period, then just grab the most recent 3 items.

I can do this easily enough using an IF/COUNT statement and running the 2 queries seperately, but wondered if it is possible to run the above logic as a single statement?
0
Comment
Question by:Rouchie
[X]
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
7 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22668625
it will be a lot easier if you post some sort of table structure on here for us to query.
0
 
LVL 7

Expert Comment

by:Norush
ID: 22668631
Can you show us the queries you are using at the moment?

We can improve them to the 1 query you want then.
0
 
LVL 25

Author Comment

by:Rouchie
ID: 22668663
Hi

These are the only 2 columns I need to retrieve data from.

[tblNews]
        |-- [datePosted]  datetime
        |-- [newsTitle]     nvarchar(200)
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 4

Assisted Solution

by:GO-87
GO-87 earned 250 total points
ID: 22668694
Would this work?
 

SELECT TOP 3 NewsItemID
FROM tblNewsItems
WHERE NewsItemDate > DateAdd(month, -3, getdate())
ORDER BY NewsItemDate DESC

Open in new window

0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 250 total points
ID: 22668692
select top 3 newsTitle
from tblNews
where datePosted > dateadd(month, -3, getdate())
order by datePosted desc
0
 
LVL 4

Expert Comment

by:GO-87
ID: 22668708
Ha ha, snap!
0
 
LVL 25

Author Comment

by:Rouchie
ID: 22668753
Okay that's exactly what I've got at the moment.

The problem is that the front page of their web site has a space for news items.  So, if they get lazy (or nothing happens worth reporting on) and they don't add any news items for a few months, then the news area would show up empty.
Therefore I was wanting to run some kind of check to grab old news if no news existed!

Thinking about it though its probably easiest to just grab the top 3 items and forget the whole date thing...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

688 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