Comment on page
Running queries from Builder
To run this example you'll need:
- An active CARTO organization
Once you have gained access to the Analytics Toolbox through the Snowflake Data Marketplace (see Getting Access for details), you can create custom SQL layers in Builder that make use of any of the available spatial functions.
To get started, let’s run a simple example query to cluster a set of points using the
ST_CLUSTERKMEANS
function from the clustering module.- 1.Click on the Add source from button in Builder, that can be found at the bottom left of the screen.
- 2.Select the second tab Custom Query (SQL) and pick the Snowflake connection that you will use to run the query. Please make sure this connection has access to the Analytics Toolbox database.
- 3.Click on Add source. A SQL console will be displayed.
- 4.Copy and paste the following query:WITH data AS(SELECT geogFROM MYDB.MYSCHEMA.starbucks_locations_usaWHERE geog IS NOT nullORDER BY id),clustered_points AS(SELECT CARTO.CARTO.ST_CLUSTERKMEANS(ARRAY_AGG(ST_ASGEOJSON(geog)::STRING), 8) AS cluster_arrFROM data)SELECT GET(VALUE, 'cluster') AS cluster, TO_GEOGRAPHY(GET(VALUE, 'geom')) AS geomFROM clustered_points, lateral FLATTEN(input => cluster_arr)
- 5.Run the query. This query computes eight clusters from the points of the
starbucks_locations_usa
table, provided as sample data within the Analytics Toolbox. As a result, each point is assigned acluster
ID. - 6.Style the layer by the
cluster
attribute.

For more examples, visit the Examples section or try executing any of the queries included in every function definition in the SQL Reference.
Last modified 5mo ago