cjard
June 15th, 2005, 05:07 PM
suppose i have a table that has 3 columns
postcode1, postcode2, distance
and for every uk address if has the distance between the 2 postcodes. its actually a view that calculates the distance, but thats inconsequential
now suppose i say:
select * from tblPostcodes where postcode1 in ('SW1','SW2') and postcode2 in ('SW3','SW4')
i will get:
SW2 SW4 1.69705627484771405856202646905163769428
SW1 SW3 1.93132079158279658385874750877742036453
SW1 SW4 3.91152144312158922874978293445751064992
SW2 SW3 5.5
now i know why i get 4 results, but i actually only want 2.
i want to group by postcode1, and select the min(distance)
BUT, i do also want to know the pcode2 that relates to these pair!
what's the generally accepted method for doing this? leaving one column out of a grouping expression but showing it infix with its relevant row in the output
clasically i do this with a self join; i select only the pcode1 and the distance using a group, then self join it back to the table, and pull the pcode2:
select *
from
postcodes,
(select min(distance), pcode1 from.. where.. in .. and ... in ... group by ...) finder
where postcodes.pcode1 = finder.pcode1 and postcodes.distance = finder.distance
but its SLOOOOOOW... remember that this isa view, and it seems that when you self join a view in oracle, the time needed rises exponentially. so is there any way? this kind of thing must be needed all the time!
postcode1, postcode2, distance
and for every uk address if has the distance between the 2 postcodes. its actually a view that calculates the distance, but thats inconsequential
now suppose i say:
select * from tblPostcodes where postcode1 in ('SW1','SW2') and postcode2 in ('SW3','SW4')
i will get:
SW2 SW4 1.69705627484771405856202646905163769428
SW1 SW3 1.93132079158279658385874750877742036453
SW1 SW4 3.91152144312158922874978293445751064992
SW2 SW3 5.5
now i know why i get 4 results, but i actually only want 2.
i want to group by postcode1, and select the min(distance)
BUT, i do also want to know the pcode2 that relates to these pair!
what's the generally accepted method for doing this? leaving one column out of a grouping expression but showing it infix with its relevant row in the output
clasically i do this with a self join; i select only the pcode1 and the distance using a group, then self join it back to the table, and pull the pcode2:
select *
from
postcodes,
(select min(distance), pcode1 from.. where.. in .. and ... in ... group by ...) finder
where postcodes.pcode1 = finder.pcode1 and postcodes.distance = finder.distance
but its SLOOOOOOW... remember that this isa view, and it seems that when you self join a view in oracle, the time needed rises exponentially. so is there any way? this kind of thing must be needed all the time!