Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need the max 2 of a group

Status
Not open for further replies.

corneI

Programmer
May 5, 2003
18
ZA
Hi all..

My problem is a very simple and stupid one. I need the top 2 values per group. For example

Group Value
1 10
1 20
1 30
2 100
2 200
2 100
2 50

What I need is.

Group Value
1 30
1 20
2 200
2 100

Is their any why to do this?
I looked at thread183-741060 but it didn't help much because I am very new to SQL
PS. My table looks the same a above example.

Thanks
:)
 
select *
from tbl t1
where Value in (select top 2 t2.Value from tbl t2 where t1.Group = t2.Group order by t2.Value desc)

======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Thanks nigelrivett for your quick reply!!! :)

But I only have one table that looks the same as the example!

Thanks
:)
 
The query only uses one table - called tbl. Replace tbl with your table name.

======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Thanks nigelrivett.

A real blond moment!!!!! :)
Stupid me!
It works wonderfully!!!!!!

Thanks a lot!!!!!!!!!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top