Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using most recently dated record in a larger query

Status
Not open for further replies.

DCurry

Programmer
Sep 1, 2004
1
US
In MS SQL Server 2000, I have a database with multiple tables, including a table 'Event_Times' with columns Event_ID & Event_Date_Time. There can be many Event_Date_Time entries for each Event_ID, tracking the times when a particular event occurred and reoccurred.

The 'Event_ID' column is a key that's used in other tables.

I want to find the Event_ID of records where the LAST (most recent) Event_Date_Time is more than 30 days ago, and then I wish to use that Event_ID as part of a larger query. It seems that I could get what I want by using the ORDER BY clause, but that's permitted in subqueries and similar structures only if a TOP clause is also part of the Select, and that restriction seems to negate its usefulness in my application. I'm stuck. Please help.
 
Code:
set @yourDate = dateadd(day,-30,getdate())
select event_id from t
group by event_ID
having max(event_date_time) < @yourdate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top