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)
 how can retrive top 5 id wise

Author  Topic 

pmotewar
Yak Posting Veteran

62 Posts

Posted - 2009-05-11 : 08:46:42
[code]ID VALUE
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
2 21
2 22
2 23
2 24
2 25
2 26
2 27
2 28
2 29
2 30
2 31
2 32
2 33
3 34
3 35
3 36
3 37
3 38
3 39
3 40
3 41
3 42
3 43
3 44
4 45
4 46
4 47
4 48
4 49
4 50
4 51
4 52
4 53
4 54
4 55


hi, above is my table data ( test data) and i want top 5 from each id in single query how can i write the query ?

any help appriciated.
[/code]

Pankaj

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-05-11 : 08:49:09
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

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-05-11 : 08:58:31
select id, valueid from (select row_number() over(partition by id order by id) as rid ,* from urtablename )s where rid <=5
Go to Top of Page

pmotewar
Yak Posting Veteran

62 Posts

Posted - 2009-05-11 : 09:24:42
quote:
Originally posted by bklr

select id, valueid from (select row_number() over(partition by id order by id) as rid ,* from urtablename )s where rid <=5




Thank you very much it's working fine

thanks again

Pankaj
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-05-11 : 09:32:47
quote:
Originally posted by pmotewar

quote:
Originally posted by bklr

select id, valueid from (select row_number() over(partition by id order by id) as rid ,* from urtablename )s where rid <=5




Thank you very much it's working fine

thanks again

Pankaj



ur welcome
Go to Top of Page
   

- Advertisement -