Nearest store

Amperity can be configured to calculate the store that is nearest to an individual customer, as represented by their Amperity ID.

Important

Nearest store calculations must be enabled for use in Amperity. This effort typically requires some customization and is not the same across tenants.

Contact your Amperity support representative via the Amperity Support Portal (or send email to support@amperity.com) to discuss your options for adding nearest store capabilities to your tenant.

Requirements

Calculating nearest store requires data to be provided to Amperity that includes the latitude and longitude for each store location. Amperity then uses a publicly available dataset to convert home zip codes to latitude and longitude. Amperity then uses the geographic center of each zip code to calculate the straight-line distance for the store nearest to the individual.

Calculated fields

For each record in the customer 360 database, the following fields are added:

  • nearest_store_id

  • nearest_store_name

  • nearest_store_distance_miles

Example

A very simple example of calculating nearest stores starts with a common table expression that uses a Haversine formula to calculate latitude and longitude. United States zip codes are joined to a table that contains data about the physical location of stores, after which stores are ranked by distance. The store with the highest rank is the closest store.

Caution

This example is appropriate for use with small, static data sources, or for defining a proof of concept for early iteration that will require a more complex workflow.

 1WITH
 2  distance_cte AS (
 3    SELECT
 4      2 * 3959 * asin(
 5        sqrt(
 6          power(
 7            sin((
 8              radians(CAST(stores.Latitude AS decimal(15, 10))) -
 9              radians(CAST(census.latitude AS decimal(15, 10))))
10            / 2), 2)
11          + cos(radians(CAST(stores.Latitude AS decimal(15, 10))))
12          * cos(radians(CAST(census.latitude AS decimal(15, 10))))
13          * power(
14            sin((
15              radians(CAST(stores.Longitude AS decimal(15, 10))) -
16              radians(CAST(census.longitude AS decimal(15, 10))))
17            / 2), 2)
18          )
19        ) distance
20      ,census.zip_code
21      ,stores.store_id
22      ,stores.name
23    FROM Stores_Table stores
24    CROSS JOIN Census_Table census
25    WHERE stores.Residence = true
26  ),
27  ranked_distance AS (
28    SELECT
29      zip_code
30      ,store_id
31      ,distance
32      ,`name`
33    RANK() OVER (PARTITION BY zip_code ORDER BY distance asc, store_id) AS rank
34    FROM distance_cte
35    WHERE distance IS NOT NULL
36  )
37
38SELECT
39  *
40FROM ranked_distance
41WHERE rank = 1