|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
SQL query helpif distance is not nullable,
select ID, distance from s where not exists(select 1 from s s1 where s1.id=s.id and s1.distance<s.distance) how do you know that null is the minimum id there are toher rows with id=4? worked for me:
create table #t(id int, distance int) insert into #t values(1, 5) insert into #t values(1, 1) insert into #t values(2, 2) insert into #t values(2, 3) insert into #t values(3, null) select ID, distance from #t s where not exists(select 1 from #t s1 where s1.id=s.id and s1.distance<s.distance) ID distance ----------- ----------- 1 1 2 2 3 NULL (3 row(s) affected) drop table #t On Fri, 30 Sep 2005 15:52:44 +0100, Mike Read wrote:
Show quote >Hi Hi Mike,> >I have a table of IDs and distances eg > >ID distance >1 0.2 >1 0.1 >2 0.3 >2 0.35 >3 0.2 >3 0.2 >4 null > >I like to pull out the row for each ID having the minimum distance (for a >given ID). > >i.e I'd like my resultset to be > >1 0.1 >2 0.3 >3 0.2 >4 null > >Can anyone help with the required SQL. > >Thanks > Mike SELECT ID, MIN(distance) FROM YourTable ? Best, Hugo -- (Remove _NO_ and _SPAM_ to get my e-mail address) |
|||||||||||||||||||||||