DISTINCT qualifier
Overview
When using aggregation functions, they can contain the DISTINCT
keyword. It acts as a qualifier for them, to ensure that only unique values are being processed. Here’s how a sample syntax looks like:
DISTINCT
keyword can be combined with the following aggregate functions:
AVG()
COUNT()
MAX()
MIN()
SUM()
All functions listed above, operate on the same input and return types, that are supported by their counterparts without any qualifiers. They can be grouped without any limitations, provided that they utilise a single DISTINCT
keyword.
DISTINCT
qualifiers used in a query with GROUP BY
statements are not supported at the moment, however if the query doesn’t include such statements, it will get executed but the output’s column order will be reversed at the momentExamples
In this section we’ll focus on a few examples, that showcase sample usage of the above mentioned concepts. They will be based on creation of the following tables:
Here’s how the created tables will look like, respectively:
DISTINCT
combined with COUNT
function
The following example uses DISTINCT
qualifier combined with COUNT()
function to calculate the number of unique car brands in rentals:
When executing the above code, it will return the following output:
Here’s another example, that uses DISTINCT
qualifier combined with COUNT()
function to calculate the amount of rentals by each customer:
It will calculate the rental_count
by each customer_name
as shown below:
DISTINCT
combined with MAX()
function
The following example uses DISTINCT
qualifier combined with MAX()
function to find maximum single spending per each customer, dropping any repeated transactions:
The output for that code will be as follows:
DISTINCT
combined with SUM()
function
The following example compares the sum of unique revenues versus the sum of all revenues in rental data:
Here’s the ouput of the above query:
The result may help to understand what is the impact of repeating transactions on total revenue.
Limitations
There are few use-cases that we are aware of but do not support currently:
- Aggregation functions with
DISTINCT
keyword used as an argument of an expression, e.g.
- Any type of multiple
DISTINCT
qualifiers used in a query withGROUP BY
statements