Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Picking earliest date from table with multiple ent

Author  Topic 

Lionheart
Starting Member

41 Posts

Posted - 2009-03-02 : 11:28:41
I have a table which contains information such as the following: -

ID Date
1 2009-2-24
1 2009-2-25
1 2009-2-26
2 2009-1-21
2 2009-2-24
2 2009-2-28
3 2008-12-29
3 2009-1-2
etc.

This records when another table linked to ID changes for thousands of ID's. What I need to do is select the earliest date for each ID and the ID for the last 30 days only. How do I go about doing this?

Thx

LH

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2009-03-02 : 11:31:15
select ID, min(Date) from table where date > dateadd(dd,0,datediff(dd,0,dateadd(dd,-30,getdate())))
group by ID
Go to Top of Page

Lionheart
Starting Member

41 Posts

Posted - 2009-03-02 : 11:37:02
Thank you very much.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-02 : 12:42:53
quote:
Originally posted by RickD

select ID, min(Date) from table where date > dateadd(dd,0,datediff(dd,0,dateadd(dd,-30,getdate())))
group by ID


the line in blue can be simplified as

dateadd(dd,datediff(dd,0,getdate())-30,0)
Go to Top of Page
   

- Advertisement -