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)
 Correlated query

Author  Topic 

Bartroels
Starting Member

1 Post

Posted - 2014-06-25 : 03:51:19
I have a list with items and their invoiced price and date:

for example
ITEM1 1.45 13/2/14
ITEM2 5.88 4/4/14
ITEM1 1.50 19/4/14
ITEM3 4.44 15/3/14
ITEM2 6.00 19/5/14
ITEM1 1.48 13/3/14
ITEM3 4.24 12/5/14

I want now for EACH item the price at the most recent date -->
ITEM1 1.50 19/4/14
ITEM2 6.00 19/5/14
ITEM3 4.24 12/5/14

I tried several ways, with correlated query, HAVING, WHERE,... but I don't reach the desired result

Can anyone help me, I think the solution is simple but I don't see it!

thanks!!
Bart

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-06-25 : 04:09:13
select * from
(select row_number() over (partition by item order by invoicedate desc) as rn,* from yourtable) as dt
where rn = 1



Too old to Rock'n'Roll too young to die.
Go to Top of Page
   

- Advertisement -