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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Get SecID, price and Date for Max(Date)

Author  Topic 

lesotho72
Starting Member

1 Post

Posted - 2007-06-06 : 17:52:15
Hello,

I am trying to get the price for the MAX (Date) for the following list:
SecID Price Date
IBM 84.23 1/1/2007
IBM 83.21 1/2/2007
IBM 85.16 1/3/2007

So, SQL should return IBM, 85.16, 1/3/2007


I cannot create a stored procedure or function to do this. Must be a view. Looking at derived tables and correlated subqueries. Realize it looks pretty easy.
Any help greatly appreciated

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-06 : 18:36:20
[code]
select T1.SecId, T2.MaxDate, T1.price
from YourTable T1
Join ( Select SecId, Max(date) MaxDate
From YourTable
Group by SecId
) T2
On T1.SecId= T2.SecIdand T1.Date = T2.MaxDate
[/code]

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-07 : 10:43:08
If you need only record of maximum date, then

Select top 1 columns from table
order by date desc

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -