AVG

Aggregate function

Description

The AVG function returns the average (arithmetic mean) of the input expression values.

(Redshift SQL Language Reference AVG function)

This function is fully supported by Snowflake.

For more information about quoted identifiers in functions, click here.

Grammar Syntax

AVG ( [ DISTINCT | ALL ] expression )

Sample Source Patterns

Setup data

CREATE TABLE example_table (
    my_smallint_column SMALLINT,
    my_integer_column INTEGER,
    my_bigint_column BIGINT,
    my_numeric_column NUMERIC,
    my_decimal_column DECIMAL,
    my_real_column REAL,
    my_double_precision_column DOUBLE PRECISION,
    my_super_column SUPER
);


INSERT INTO example_table (
    my_smallint_column,
    my_integer_column,
    my_bigint_column,
    my_numeric_column,
    my_decimal_column,
    my_real_column,
    my_double_precision_column,
    my_super_column
)
VALUES
    (1, 100, 10000000000, 123.45, 678.90, 3.14, 2.71828, 123),
    (2, 200, 20000000000, 234.56, 789.01, 2.71, 3.14159, 456),
    (3, 300, 30000000000, 345.67, 890.12, 1.41, 1.61803, 789),
    (4, 400, 40000000000, 456.78, 901.23, 1.61, 1.41421, 101112),
    (5, 500, 50000000000, 567.89, 123.45, 2.17, 3.14159, 131415);

Input Code:

IN -> Redshift_01.sql
SELECT
    AVG(DISTINCT my_smallint_column) AS type_smallint,
    AVG(ALL my_integer_column) AS type_integer,
    AVG(my_bigint_column) AS type_bigint,
    AVG(my_numeric_column) AS type_numeric,
    AVG(my_decimal_column) AS type_decimal,
    AVG(my_real_column) AS type_real,
    AVG(my_double_precision_column) AS type_double_precision,
    AVG(my_super_column) AS type_super
FROM example_table;

Output Code:

OUT -> Redshift_01.sql
SELECT
     AVG(DISTINCT my_smallint_column) AS type_smallint,
     AVG(ALL my_integer_column) AS type_integer,
     AVG(my_bigint_column) AS type_bigint,
     AVG(my_numeric_column) AS type_numeric,
     AVG(my_decimal_column) AS type_decimal,
     AVG(my_real_column) AS type_real,
     AVG(my_double_precision_column) AS type_double_precision,
     AVG(my_super_column) AS type_super
FROM example_table;

Note

AVG, depending on the data type, can behave differently in terms of rounding and formatting, which may result in different precision or decimal places when comparing Redshift with Snowflake.

There are no known issues.

Last updated