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)
 DISTINCT record?

Author  Topic 

wyerarch
Starting Member

2 Posts

Posted - 2009-06-16 : 08:15:42
Hi All,

I am retrieving data from my sql db, which represents distances from customers. The problem is, each customer can register more than one address/postcode.

What I am doing is looking up the logitude and latitude of the postcode, and calculating the distance from the target. What I get back is something like:

ID Customer Distance
1 A 2
1 A 5
2 B 6
2 B 19

What I need is:

ID Customer Distance
1 A 2
2 B 6

I.e. Out of this data, retrieve the first, closest record by distance.

Does anyone know how please?

Many thanks in advance.

James.

nr
SQLTeam MVY

12543 Posts

Posted - 2009-06-16 : 08:18:56
select id, customer, distance = min(distance)
from tbl
group by id, customer


==========================================
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.
Go to Top of Page

wyerarch
Starting Member

2 Posts

Posted - 2009-06-16 : 09:09:55
Thanks nr, that worked a treat!
Go to Top of Page
   

- Advertisement -