I am trying to build a rather complicated query in SQL, and being a beginner i would immensely appreciate some help to build it.
I am trying to achieve the following:
1/ Calculate the distance
between a postcode in the target_postcodes table - say E1 1AA - and all the postcodes in the the population_postcodes table using Cartesian latitude and longitude coordinates using Pythagoras:
SQRT( POW(MY_Y_AXIS - Y_AXIS, 2) + POW(MY_X_AXIS-X_AXIS, 2) )
2/ Create a new column with those distance
values,
not sure how to do that step
2-bis/ Sort postcodes in the population_postcodes
by the distance
value we obtained,
not sure how to do that step
3/ Beginning with the closest postcode, add the value in the population column to a running_count column UNTIL running_count
> Number_of_beds
of E1 1AA,
proposed query for running count - but missing the above breaking condition:
SELECT distance, Population,
(SELECT sum(population_postcodes.Population)) AS Total
FROM population_postcodes
WHERE population_postcodes.distance <= T1.distance) AS Total
FROM population_postcodes AS T1
4/ Create a new table that contains the postcode E1 1AA (target_postcode
) and the distance value of the last postcode added to our running count.
Finally, i would need to loop this query over the whole target_postcodes
table.
Thank you for you very much for helping a newbie out!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…