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)
 Select Statement

Author  Topic 

galbrecht
Starting Member

17 Posts

Posted - 2008-05-05 : 17:18:17
Hi Guys

I have a table with a field that holds telephone no's eg:

Name , Day_Tel_No


Greg , 07 2376543
Greg , 07 2456689

some names have more than one telephone no.
I would like to extract only the latest one (the top no). Can anyone shed some light on how to write this statement.

Thanks

Greg

karuna
Aged Yak Warrior

582 Posts

Posted - 2008-05-05 : 17:30:24
How do you find out which is the latest one? Is there a column which stores this information?

Thanks
Karunakaran
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-06 : 00:25:00
quote:
Originally posted by galbrecht

Hi Guys

I have a table with a field that holds telephone no's eg:

Name , Day_Tel_No


Greg , 07 2376543
Greg , 07 2456689

some names have more than one telephone no.
I would like to extract only the latest one (the top no). Can anyone shed some light on how to write this statement.

Thanks

Greg


SELECT t.Name,t.Day_Tel_No
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Day_Tel_No) AS RowNo,
Name,
Day_Tel_No
FROM YourTable)t
WHERE t.RowNo=1
Go to Top of Page

soorajtnpki
Posting Yak Master

231 Posts

Posted - 2008-05-06 : 03:14:48
hi,
you can try this if u have table_id in ur table

select * from table where table_id in
(select max(table_id) from table group by name)

ok
tanx...
Go to Top of Page
   

- Advertisement -