Modeling SQL Query to add statistical data to a geometryDo you have to do it in SQL? It could be easier to do it in something like R, where you connect to the SQL DB, and have the ability to store intermediate results in dataframes throughout a script. Not saying it can't be done in SQL but long term, it might be more legible in another language.