Ask or search…
K
Links
Comment on page

Running queries from Builder

Requirements

To run this example you'll need:

Guide

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. 1.
    Click on the Add source from button in Builder, that can be found at the bottom left of the screen.
  2. 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. 3.
    Click on Add source. A SQL console will be displayed.
  4. 4.
    Copy and paste the following query:
    WITH data AS(
    SELECT geog
    FROM MYDB.MYSCHEMA.starbucks_locations_usa
    WHERE geog IS NOT null
    ORDER BY id
    ),
    clustered_points AS
    (
    SELECT CARTO.CARTO.ST_CLUSTERKMEANS(ARRAY_AGG(ST_ASGEOJSON(geog)::STRING), 8) AS cluster_arr
    FROM data
    )
    SELECT GET(VALUE, 'cluster') AS cluster, TO_GEOGRAPHY(GET(VALUE, 'geom')) AS geom
    FROM clustered_points, lateral FLATTEN(input => cluster_arr)
  5. 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 a cluster ID.
  6. 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.