Amazon Marketing Cloud · SQL
AMC SQL Function Reference
Syntax, quirks and an example for the AMC SQL functions that show up most in queries. Signatures come from the Amazon documentation, the explanations are ours. The examples are checked against our schema reference but not executed on an AMC instance.
Date and time
-
DATE_TRUNCDATE_TRUNC cuts a timestamp down to a unit such as week or month, so events can be grouped into periods.
-
EXTRACTEXTRACT reads exactly one time unit from a DATE or TIMESTAMP, for example the hour or the day of week.
-
CONVERT_TIME_ZONE_FROM_UTCCONVERT_TIME_ZONE_FROM_UTC converts a UTC timestamp to another time zone such as Europe/Berlin.
-
EXTEND_TIME_WINDOWEXTEND_TIME_WINDOW widens the period of an input table beyond the reporting window of the query, backwards and forwards.
-
SECONDS_BETWEENSECONDS_BETWEEN returns the seconds between two timestamps, computed as the second value minus the first.
Aggregation
-
APPROX_COUNT_DISTINCTAPPROX_COUNT_DISTINCT counts the distinct values of a column approximately and is faster than COUNT(DISTINCT) on large data.
-
COLLECTCOLLECT gathers the values of a column per group into an array; with DISTINCT only the distinct values.
Arrays
-
ARRAY_CONTAINSARRAY_CONTAINS checks whether a value occurs in an array and returns true or false.
-
ARRAY_INTERSECTARRAY_INTERSECT returns a new array with the elements that appear in both input arrays.
-
ARRAY_TO_STRINGARRAY_TO_STRING joins the elements of an array into one string, separated by a delimiter. It is the documented counterpart of ARRAY_JOIN.
-
EXPLODEEXPLODE turns an array into multiple rows, one row per element. It is the reverse of COLLECT.
-
CARDINALITYCARDINALITY returns the number of elements in an array, for example how many values COLLECT gathered per group.
Conditionals
Window functions
-
LAGLAG reads the value of an earlier row in the same partition without joining the table to itself.
-
ROW_NUMBERROW_NUMBER numbers the rows of a partition consecutively in the order of the OVER clause.
Statistics
-
PERCENTILEPERCENTILE returns the value at a given point of a distribution, such as the 90th percentile of click cost. APPROX_PERCENTILE computes it approximately.
Other functions
-
NAMED_ROWNAMED_ROW groups several columns under one name, each with its own label, in a single result column.