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)
 display top 1 record grouped by name

Author  Topic 

jamie
Aged Yak Warrior

542 Posts

Posted - 2004-10-28 : 09:20:25
hello,
how can I display the top one record and group by something .
eg.
value name
200 jamie
100 jamie
300 bob
250 bob


I want to show :

value name
200 jamie
300 bob


is that posible ?

thank you,
Jamie

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-10-28 : 09:43:21
this has got to be one of the top 5 most asked questions of all time...
it is possible:

select t1.*
from table1 t1
inner join (select max(value) as value, name from table1 group by name ) t2 on t1.value = t2.value and t1.name = t2.name

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

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-10-28 : 09:46:16
you might be able to get away with:

select name, max(value)
from tbl
group by name

if you just want the maximum of "value" to be returned, per name.



- Jeff
Go to Top of Page

jamie
Aged Yak Warrior

542 Posts

Posted - 2004-10-28 : 10:16:53
hi, thanks for the reponses.
however I made a mistake.
my data is like :

name value date
jamie 300 30/06/04
jamie 200 10/06/04
bob 250 04/06/04
bob 100 15/06/04


what I need is to get the value for the max date for each name...
I have tried max(date), value, but I still get all the results.

Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-10-28 : 10:17:41
change value to date in my query

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

jamie
Aged Yak Warrior

542 Posts

Posted - 2004-10-28 : 10:29:28
that doesn't work , I still get more than one row for some names. however, Ithink I have managed to do it by creating a query for maxofdate, then linking this query to the original query by name and date. seems to be ok.
however I'll b back if not ! :o)

ps, I am using access2000, I should have said that to begin with. sorry.
Go to Top of Page
   

- Advertisement -