DDSQL Functions

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!
Join the Beta!

DDSQL is in private beta.

Request Access

Aggregation functions

Aggregate functions compute a single result from a set of input values, usually used in conjunction with a GROUP BY statement.

avg

NameArgument TypesReturn typeDescription
avg(expr e)numericnumericComputes the average (arithmetic mean) of all the non-null input values.

max

NameArgument TypesReturn typeDescription
max(expr e)variablevariableComputes the maximum of the non-null input values. Types of input values must be comparable.

min

NameArgument TypesReturn typeDescription
min(expr e)variablevariableComputes the minimum of the non-null input values. Types of input values must be comparable.

sum

NameArgument TypesReturn typeDescription
sum(expr e)numericnumericComputes the sum of the non-null input values.

count

NameArgument TypesReturn typeDescription
count(expr e)numericintegerComputes the number of input rows in which the input value is not null.
count(distinct expr e1, e2 …)integerComputes the number of input values in which the input value is not null.
count(*)integerComputes the number of input rows.

string_agg

NameArgument TypesReturn typeDescription
string_agg(expr e, delimiter e)string, stringstringConcatenates the input values, seperated by a delimiter.

array_agg

NameArgument TypesReturn typeDescription
string_agg(expr e)variablearrayConcatenates the input values into an array.

Scalar functions

These functions return one value per row.

String functions and operators

NameReturn typeDescription
upper(text s)textConverts s to uppercase.
lower(text s)textConverts s to lowercase.
length(text s)integerCounts the number of characters in s.
concat(expr x, y, …)textConcatenates the provided expressions.
substr(expr s, numeric start, numeric numChars)textReturns a substring of s from start to a max of numChars, if provided. start is a 1-based index, so substr('hello', 2) returns 'ello'. If the start is less than 1, it is treated as if it were 1. The result is computed by taking the range of characters [start, start+numChars], where if any value is less than 1, it is treated as 1. This means substr('hello', -2, 4) returns 'h'.
replace(text s, text from, text to)textReplaces all occurrences in s of substring from with substring to.
regexp_replace(text s, text pattern, text replacement)textReplace substrings in s that match the POSIX regular expression pattern with the replacement. Supports Go’s regular expression syntax.
reverse(expr text)stringReverses the string (brown → nworb).
md5(expr text)stringCalculates the MD5 hash of a string and returns the result in hexadecimal.
char_length(str text)integerReturns number of characters in str.
left(str text, n int)textReturns first n characters in str. When n is negative, return all but last |n| characters.
right(str text, n int)textReturns last n characters in str. When n is negative, return all but first |n| characters.
ltrim(str text [, characters text])textRemoves the longest string containing only characters from characters (a space by default) from the start of str.
rtrim(str text [, characters text])textRemoves the longest string containing only characters from characters (a space by default) from the end of str
trim([leading | trailing | both] [characters] from str)textRemoves the longest string containing only the characters (a space by default) from the start/end/both ends of str.
sort_order_ip(ip text)textReturns a string representing a sort order over IPv4 and IPv6 range.

Mathematical functions and operators

NameReturn typeDescription
abs(numeric n)integerReturns the absolute value of n.
round(numeric n, [s])numericRound n to s decimal places.
mod(numeric x, numeric y)integerReturns the remainder of x / y.
floor(numeric n)numericReturns the nearest integer that is less than or equal to n.
ceil(numeric n)numericReturns the nearest integer that is greater than or equal to n.
power(numeric n, numeric s)numericRaises n to the s power.
ln(numeric n)numericCalculates the natural logarithm of n.
log(numeric n)numericCalculates the logarithm to base 10 of n.
log2(numeric n)numericCalculates the logarithm to base 2 of n.
exp(numeric n)numericReturns the mathematical constant e, raised to the power of n.
sqrt(numeric n)numericCalculates the square root of n.

Array functions and operators

NameReturn typeDescription
array_length(array a)integerReturns the length of the array a for each row.
array_contains(array a, expr e)booleanReturns true if the value the expr e evaluates to is in the array a for each row.
array_cat(array a, array b)arrayReturns a new array containing the combined elements from array a and array b.
array_append(array a, expr e)arrayReturns a new array that includes all the original elements of the input array followed by the appended element.
string_to_array(text s, delimiter, [,nullString])arrayReturns an array of substrings obtained by splitting the input string s, using the specified delimiter. The third argument, nullString, is optional and specifies substrings that are replaced with NULL.
array_to_string(array a, delimiter, [,nullString])stringConcatenates array elements using supplied delimiter and optional null string.
unnest(array a)variableReturns each element in the array as a separate row. The return type is the element type of the array.
unnest can only be used in the SELECT clause of a query. If other columns are SELECTed with unnest, the value at each row in the table is repeated at each output row with each unnested element. If multiple columns are being unnested, all the unnested columns are zipped up together, with NULL filling in the output values for shorter arrays.

Date/time functions and operators

NameReturn typeDescription
date_trunc(string precision, timestamp t)timestampTruncates the timestamp to the chosen precision (“second”, “minute”, “hour”, “day”, “week”, “month”, or “year”).
date_diff(string precision, timestamp t, timestamp t)integerReturns the difference between two dates, in the precision specified.
to_timestamp(numeric n)timestampTransforms n into a timestamp, considering n as the time in seconds.

Conditional expressions

NameReturn typeDescription
coalesce(expr x, y, …)variableReturns the first non-null expression.
nullif(expr x, expr y)variableReturns NULL if both arguments are equal. Otherwise, returns x.

JSON functions and operators

NameReturn typeDescription
json_extract_path_text(text json, text path…)textExtracts the JSON sub-object in JSON as text, defined by the path. Its behavior is equivalent to the postgres function with the same name. For example, json_extract_path_text(col, ‘forest') returns the value of the key forest for each JSON object in col. See the example below for a JSON array syntax.
json_extract_path(text json, text path…)jsonSame functionality as json_extract_path_text, but returns a column of JSON type instead of text type.
json_build_object(key1 text, value1 json/text/int/float, key2 text, value2 json/text/int/float, … )jsonBuilds a JSON object based on the parameters passed in. The parameters to the function are the keys/values of the JSON object being built, alternating between key and value mapped to each key.
row_to_json(table)jsonReturns a JSON representation of each row in a table as a JSON value. The JSON keys are the column names, and the values are the values under each row at each column.

Note: row_to_json takes in a table name, NOT a column, for example, SELECT row_to_json(<table>) FROM <table>.

JSON array

Return the value of the key forest in the 0th element in a JSON array for each JSON object or row in col.

[{
"forest": "trees"
}]
json_extract_path_text(col, ‘0', ‘forest')
PREVIEWING: rtrieu/product-analytics-ui-changes