[SQL] howto?

2003-03-17 Thread Sjors



Hi,
 
Being not able to grasp in full sql, I've decided 
to call on the list. I've got a table with points, place_id and distance between 
points and place_id. For each point there can be more place_id's but I want 
to selesct the one with the minimum distance. Now I can group by the points 
and select the min(distance) but I can't do this if place_id is involved. I used 
a statment like this:
 
Select points, place_id, distance from 
table
group by points, place_id, distance
having count(*)>1 and distance=(select 
min(distance) from table
 
This will give me 1 value being the 
minimum of all distances. 
If only done on basis of points and distance I do 
get the minimum values, but then I don't have the place_id. 
How can I get the minimum values of the 
place_id within in the group of points, being the same? 
I've looked and played around with variations 
round but I can't figure out how to do this. Any 
suggestions??  
 
greetz sjors


Re: [SQL] howto/min values

2003-03-19 Thread Sjors



Hi,
 
My postings seem to double on me. Well thanks 
to everybody that answered. They were not the answers I was hoping for 
but, I found one in the docs. Let me explain again what I'm doing with a 
example:
 
table:
 
points    id    
distance
1        
    25    0.26598333
1        
    32    0.65983600    

1        
    56    1.23692000
2        
    96    120.26659366
2        
    32    0.54865130
etc
 
Now select for point 1 where id = min(distance). 
The trick I was suspecting but not been able to grasp is the use of aliases. In 
Bruce Momjian's "Postgresql: introduction and limitations" was an example like 
this with structure from the problem here:
 
select p.points, p.id, p.distance 
from table as p
where distance = (select min(distance) 

        
                
    from table as p1
        
                
    where p.points=p1points)
order by points, id
 
Now this works. So going to the next question, if 
distance is a calculated field how can I use it in the subquery. 
How do you add another alias in:
 

select p.points, p.id, (x-x) as distance 

from table as p
where distance = (select min(distance) 

        
                
    from table as p1
        
                
    where p.points=p1points)
order by points, id 
This of course doesn't work, because of the 
referring to the calculated field wich is not actually a part of the 
table. Can you do this somehow or is it impossible?
Love to hear from you guys,
 
greetz Sjors