# Counting points within range using CartoDB?

I have a dataset of users location and want to assign per user how many other users they have within a given range, and updating this value into a table.The dataset name is "users_20160410" and the column to be updated is "countdistance"

I have manage to get the number of total users within 10km for a given point, which is a unique value:

``````UPDATE users_20160410 SET countdistance = (select count(*) from users_20160410 where st_distance(the_geom,st_GeomFromText('POINT(-3.703550 40.417192)', 4326), true) < 10000)
``````

What I want is to know for every single user how many other user he has within a range of 10km (this means get a value per user, not 1 unique as per my previous query). Also as the distance is not a fixed given point, the st_Geom_From_Text could not be used as it should take the exact location per user.

## 2 Answers

You'll want to update from the results of another query. For performance it's best to do within the context of an indexed column, so you end up with some ugliness.

``````WITH sums AS (
SELECT a.cartodb_id AS cartodb_id, Count(*) as count, sum(a.popn) as popn
FROM populated_places_esp a
JOIN populated_places_esp b
ON ST_DWithin(
a.the_geom_webmercator,
b.the_geom_webmercator,
10000 / cos(radians(st_y(a.the_geom))))
GROUP BY a.cartodb_id
)
UPDATE populated_places_esp
SET countdistance = sums.count
FROM sums
WHERE sums.cartodb_id = populated_places_esp.cartodb_id;
``````

The normalization of the distance by `cos(latitude)` is to make up for distortions from the mercator projection.

• Query from @ramiroaznar works perfect thought is not updating the table. Paul, your query is giving me an error "column cartodb_id" is ambiguous. Here the used query: WITH sums AS ( SELECT a.cartodb_id AS cartodb_id, Count(*) as count, sum(a.id) as id FROM users_20160410 a JOIN users_20160410 b ON ST_DWithin( a.the_geom_webmercator, b.the_geom_webmercator, 10000 / cos(radians(st_y(a.the_geom)))) GROUP BY a.cartodb_id ) UPDATE users_20160410 SET countdistance = sums.count FROM sums WHERE sums.cartodb_id = cartodb_id – Alejandro Apr 16 '16 at 21:28

Using Spanish cities from `populated_places` as an example table, the following SQL query count the number of closest (100 Km) neighbors of each city:

``````SELECT
p.name, COUNT(*) AS num_neigh
FROM
populated_places_esp n,
populated_places_esp p
WHERE
ST_DWithin(p.the_geom::geography, n.the_geom::geography, 100000)
AND
p.cartodb_id != n.cartodb_id
GROUP BY p.name
ORDER BY num_neigh DESC
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.9152663,"math_prob":0.8244441,"size":819,"snap":"2021-04-2021-17","text_gpt3_token_len":207,"char_repetition_ratio":0.117791414,"word_repetition_ratio":0.0,"special_character_ratio":0.2881563,"punctuation_ratio":0.08176101,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9664452,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-11T12:55:54Z\",\"WARC-Record-ID\":\"<urn:uuid:a98f7ce0-c60b-43b7-8e01-8abd8a0b70d6>\",\"Content-Length\":\"172069\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d4aed22c-697a-4729-a5ac-8fe6ab60d25e>\",\"WARC-Concurrent-To\":\"<urn:uuid:92796968-e1ff-44f8-82af-7c23c280d6ec>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://gis.stackexchange.com/questions/189422/counting-points-within-range-using-cartodb\",\"WARC-Payload-Digest\":\"sha1:Y36MXBJGS5M7DSDDXU5JHZ6PFITKLNNF\",\"WARC-Block-Digest\":\"sha1:CSL2KFD6YKP4QLSWAXERAVBUN4GWA6SE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038062492.5_warc_CC-MAIN-20210411115126-20210411145126-00079.warc.gz\"}"}