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 2000 Forums
 Transact-SQL (2000)
 Selcting Top Record From Each Group

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-12-07 : 08:37:21
juno writes "Hi,

I need to fetch the top1 record from each group.

for example if i have a table like
Item1 Group1 1 SubGroup1
Item1 Group1 10 SubGroup10
Item1 Group1 23 SubGroup23
Item1 Group1 55 SubGroup55
Item3 Group3 10 SubGroup10
Item3 Group3 12 SubGroup12
Item4 Group4 10 SubGroup10
Item4 Group6 40 SubGroup50
Item5 Group5 47 SubGroup47

I want to fetch only
Item1 Group1 1 SubGroup1
Item3 Group3 10 SubGroup10
Item4 Group4 10 SubGroup10
Item5 Group5 47 SubGroup47


i need the solution in a single sql query.kindly help.

thanks"

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-12-07 : 09:03:59
ahhh...... another candidate for top 10...
select * from
MyTable t1 inner join
(select Item, min(id) as id from MyTable group by Item) t2 on t1.Item = t2.Item and t1.id = t2.id

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -