Skip to main content

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.

Examples

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 is one usecase we are aware of but do not support currently:
  • Aggregation functions with DISTINCT keyword used as an argument of an expression, e.g.