Teradata MAX is used to get the maximum value from the value_expression.
Teradata MAX Syntax
The syntax of the Teradata MAX is as follows.
MAX/MAXIMUM (DISTINCT/ALL value_expression)
- DISTINCT – If DISTINCT is used in the MAX function then all the duplicates and non-null values will be eliminated while calculating the maximum.
- ALL – This is the default value. In this case, all non-null values including duplicates will be counted while calculating the maximum for a group.
- value_expression – a literal or column expression for which the maximum needs to be computed. The expression cannot contain any ordered analytical or aggregate functions.
Teradata MAX Example
Let’s take an example to understand the concept of the Teradata MAX (maximum) function.
Let’s create a volatile table name products and insert some dummy data into it.
CREATE VOLATILE TABLE Products ( ProductID INT NOT NULL, ProductName varchar(100) NOT NULL, SupplierID INT NOT NULL, CategoryID INT NOT NULL, Unit INT NOT NULL, Price INT ) unique primary index(ProductID) on commit preserve rows;
insert into products (1,'Batteries',1,1,10,25); insert into products (2,'Wooden single drawer',1,2,20,47); insert into products (3,'Classic wooden chair',2,2,15,31); insert into products (4,'Magazine chair',1,1,10,NULL); insert into products (5,'Chair with love',2,1,20,31); insert into products (6,'Round chair',2,2,15,11);
Now check the data of the table using the select statement.
select * from Products order by 1;
1) Teradata MAX: Finding maximum value from a column
You can use the following query to find the maximum value from a column. In this example, we are calculating the maximum value from the column
select max(Price) from Products;
This can be also written as,
select maximum(Price) from Products;
2) Teradata MAX: using ALL
select max(ALL Price) from Products;
3) Teradata MAX: using DISTINCT
select max(DISTINCT Price) from Products;
4) Teradata MAX: using group by clause
select CategoryID, max(Price) from Products group by CategoryID order by CategoryID;