Select TOP 3 items based on date

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?
LVL 25
RouchieAsked:
Who is Participating?
 
momi_sabagConnect With a Mentor Commented:
select top 3 newsTitle
from tblNews
where datePosted > dateadd(month, -3, getdate())
order by datePosted desc
0
 
chapmandewCommented:
it will be a lot easier if you post some sort of table structure on here for us to query.
0
 
NorushCommented:
Can you show us the queries you are using at the moment?

We can improve them to the 1 query you want then.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
RouchieAuthor Commented:
Hi

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

[tblNews]
        |-- [datePosted]  datetime
        |-- [newsTitle]     nvarchar(200)
0
 
GO-87Connect With a Mentor Commented:
Would this work?
 

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

Open in new window

0
 
GO-87Commented:
Ha ha, snap!
0
 
RouchieAuthor Commented:
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
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.

All Courses

From novice to tech pro — start learning today.