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)
 Top 1 from group

Author  Topic 

trouble2
Constraint Violating Yak Guru

267 Posts

Posted - 2009-07-20 : 07:09:26
I know this must probably have been asked 1000s of times, but I can't find it.

I'm making an articles table with versioning, and I want to list the articles where only the most recent version of each article is shown.

The table looks like this:

ItemID _ ArticleID _ Version _ Title _ Content
1 _ 1 _ 1 _ Title1 _ Content 1, version 1
2 _ 1 _ 2 _ Title1 _ Content 1, version 2
3 _ 1 _ 3 _ Title1 _ Content 1, version 3
4 _ 2 _ 1 _ Title2 _ Content 2, Version 1
5 _ 2 _ 2 _ Title2 _ Content 2, Version 2


When I run SP called ListContent it should return:
ItemID _ ArticleID _ Version _ Title _ Content
3 _ 1 _ 3 _ Title1 _ Content 1, version 3
5 _ 2 _ 2 _ Title2 _ Content 2, Version 2

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-20 : 07:12:08
[code]
select *
from
(
select *, row_no = row_number() over (partition by ArticleID order by Version desc)
) d
where row_no = 1
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-07-20 : 08:29:05
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspx

Madhivanan

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

- Advertisement -