sql funcation 12 class

Upload: ashokkumar-kumawat

Post on 06-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 SQL Funcation 12 Class

    1/24

    ABS (T-SQL)Returns the absolute, positive value of the given numeric expression.

    Syntax

    ABS(numeric_expression)Arguments

    numeric_expression

    Is an expression of the exact numeric or approximate numeric data type category, except forthe bit data type.

    Return Types

    Returns the same type as numeric_expression.

    Examples

    This example shows the effect of the ABS function on three different numbers.SELECT ABS(-30)

    output 30

    FLOOR (T-SQL)Returns the largest integer less than or equal to the given numeric expression.

    Syntax

    FLOOR(numeric_expression)

    Arguments

    numeric_expression

    Is an expression of the exact numeric or approximate numeric data type category, except forthe bit data type.

    Return Types

    Returns the same type as numeric_expression.

    Examples

    This example shows positive numeric, negative numeric, and currency values with the FLOOR function.SELECT FLOOR(123.45)

    OutPut 123

  • 8/3/2019 SQL Funcation 12 Class

    2/24

    POWER (T-SQL)

    Returns the value of the given expression to the specified power.Syntax

    POWER(numeric_expression, y)

    Arguments

    numeric_expression

    Is an expression of the exact numeric or approximate numeric data type category, except forthe bit data type.

    y

    Is the power to which to raise numeric_expression.y can be an expression of the exactnumeric or approximate numeric data type category, except for the bit data type.

    Return Types

    Same as numeric_expression.

    Examples

    A. Use POWER to show results of 0.0

    This example shows a floating point underflow that returns a result of 0.0.SELECT POWER(4,2)

    Output 16

    ROUND (T-SQL)Returns a numeric expression, rounded to the specified length or precision.

    Syntax

    ROUND(numeric_expression, length[,function])

    Arguments

    numeric_expression

    Is an expression of the exact numeric or approximate numeric data type category, except forthe bit data type.

    length

    Is the precision to which numeric_expression is to be rounded. length must be tinyint,smallint, orint. When length is a positive number, numeric_expression is rounded to thenumber of decimal places specified by length. When length is a negative number,numeric_expression is rounded on the left side of the decimal point, as specified by length.

  • 8/3/2019 SQL Funcation 12 Class

    3/24

    function

    Is the type of operation to perform.function must be tinyint, smallint, orint. Whenfunctionis omitted or has a value of 0 (default), numeric_expression is rounded. When a value otherthan 0 is specified, numeric_expression is truncated.

    Return Types

    Returns the same type as numeric_expression.

    Remarks

    ROUND always returns a value. Iflength is negative and larger than the number of digits before the decimal point,ROUND returns 0.

    Example Result

    ROUND(748.58, -4) 0In Microsoft SQL Server version 7.0, ROUND returns a rounded numeric_expression, regardless of data type,when length is a negative number.

    Examples Result

    ROUND(748.58, -1) 750.00ROUND(748.58, -2) 700.00

    ROUND(748.58, -3) 1000.00

    Examples

    A. Use ROUND and estimates

    This example shows two expressions illustrating that with the ROUND function the last digit is always an estimate.SELECT ROUND(123.9994, 3), ROUND(123.9995, 3)

    SELECT ROUND(10.645,2)

    output 10.650

    SIGN (T-SQL)Returns the positive (+1), zero (0), or negative (-1) sign of the given expression.

    Syntax

    SIGN(numeric_expression)

    Arguments

    numeric_expression

    Is an expression of the exact numeric or approximate numeric data type category, except forthe bit data type.

    Return Types

    float

    Examples

    This example returns the SIGN values of numbers from -1 to 1.DECLARE @value realSET @value = -1WHILE @value < 2

  • 8/3/2019 SQL Funcation 12 Class

    4/24

    BEGINSELECT SIGN(@value)SET NOCOUNT ONSELECT @value = @value + 1SET NOCOUNT OFFENDSET NOCOUNT OFFGO

    Here is the result set:(1 row(s) affected)-------------------------1.0(1 row(s) affected)------------------------0.0(1 row(s) affected)------------------------1.0(1 row(s) affected)

    SELECT sign(-30)

    Output -1

    SQRT (T-SQL)Returns the square root of the given expression.

    SyntaxSQRT(float_expression)

    Arguments

    float_expression

    Is an expression of type float.

    Return Types

    float

    Examples

    This example returns the square root of numbers between 1.00 and 10.00.DECLARE @myvalue floatSET @myvalue = 1.00WHILE @myvalue < 10.00BEGINSELECT SQRT(@myvalue)SELECT @myvalue = @myvalue + 1ENDGO

    Here is the result set:

  • 8/3/2019 SQL Funcation 12 Class

    5/24

    ------------------------1.0------------------------1.4142135623731------------------------1.73205080756888------------------------2.0------------------------2.23606797749979------------------------2.44948974278318------------------------2.64575131106459------------------------2.82842712474619------------------------3.0

    SELECT sqrt(16)

    Output 4.0

    TAN (T-SQL)Returns the tangent of the input expression.

    Syntax

    TAN(float_expression)

    Arguments

    float_expression

    Is an expressionof type float orreal, interpreted as number of radians.

    Return Types

    float

    Examples

    This example returns the tangent of PI()/2.SELECT TAN(PI()/2)

    Here is the result set:----------------------1.6331778728383844E+16

  • 8/3/2019 SQL Funcation 12 Class

    6/24

    MOD() Function Returns the remainder after a number is divided by divisorSELECT (55%3)

    -----------

    Out Put 1

    (1 row(s) affected)

    CEILING (T-SQL)A mathematical function that returns the smallest integer greater than or equal to the given numeric expression.

    Syntax

    CEILING(numeric_expression)

    Arguments

    numeric_expression

    Is an expressionof the exact numeric or approximate numeric data type category, except forthe bit data type.

    Return Types

    Returns the same type as numeric_expression.

    Examples

    This example shows positive numeric, negative, and zero values with the CEILING function.

    SELECT CEILING($123.45), CEILING($-123.45), CEILING($0.0)GO

    Here is the result set:--------- --------- --------------------------124.00 -123.00 0.00(1 row(s) affected

    SELECT ceiling(10.6)

    Output 11

    CHAR (T-SQL)A string function that converts an int ASCII code to a character.

    Syntax

    CHAR(integer_expression)

    Arguments

    integer_expression

  • 8/3/2019 SQL Funcation 12 Class

    7/24

    Is an integer from 0 through 255. NULL is returned if the integer expression is not in thisrange.

    Return Types

    char(1)

    RemarksCHAR can be used to insert control characters into character strings. The table shows some commonly used controlcharacters.

    Control character Value

    Tab CHAR(9)Line feed CHAR(10)Carriage return CHAR(13)

    Examples

    A. Use ASCII and CHAR to print ASCII values from a string

    This example prints the ASCII value and character for each character in the string New Moon.SET TEXTSIZE 0

    -- Create variables for the character string and for the current-- position in the string.DECLARE @position int, @string char(8)-- Initialize the current position and the string variables.SET @position = 1SET @string = 'New Moon'WHILE @position

  • 8/3/2019 SQL Funcation 12 Class

    8/24

    LOWER (T-SQL)Returns a character expression after converting uppercase character data to lowercase.

    Syntax

    LOWER(character_expression)

    Arguments

    character_expression

    Is an expression of character or binary data. character_expression can be a constant, variable,or column. character_expression must be of a data type that is implicitly convertible tovarchar. Otherwise, use CAST to explicitly convert character_expression.

    Return Types

    varchar

    Examples

    This example uses the LOWER function, the UPPER function, and nests the UPPER function inside the LOWERfunction in selecting book titles that have prices between $11 and $20.USE pubsGOSELECT LOWER(SUBSTRING(title, 1, 20)) AS Lower,UPPER(SUBSTRING(title, 1, 20)) AS Upper,LOWER(UPPER(SUBSTRING(title, 1, 20))) As LowerUpperFROM titlesWHERE price between 11.00 and 20.00GO

    Here is the result set:Lower Upper LowerUpper-------------------- -------------------- --------------------

    the busy executive's THE BUSY EXECUTIVE'S the busy executive'scooking with compute COOKING WITH COMPUTE cooking with computestraight talk about STRAIGHT TALK ABOUT straight talk aboutsilicon valley gastr SILICON VALLEY GASTR silicon valley gastrsecrets of silicon v SECRETS OF SILICON V secrets of silicon vprolonged data depri PROLONGED DATA DEPRI prolonged data deprififty years in bucki FIFTY YEARS IN BUCKI fifty years in buckisushi, anyone? SUSHI, ANYONE? sushi, anyone?(8 row(s) affected)

    SELECT lower("GAIT COMPUTER")

    Output gait computer

    SUBSTRING (T-SQL)Returns part of a character, binary, text, or image expression. For more information about the valid Microsoft SQLServer data types that can be used with this function, see Data Types.

    http://opt/scribd/conversion/tmp/scratch6270/da-db_1.htmhttp://opt/scribd/conversion/tmp/scratch6270/da-db_1.htmhttp://opt/scribd/conversion/tmp/scratch6270/da-db_1.htm
  • 8/3/2019 SQL Funcation 12 Class

    9/24

    Syntax

    SUBSTRING(expression,start,length)

    Arguments

    expression

    Is a character string, binary string, text, image, a column, or an expression that includes acolumn. Do not use expressions that include aggregate functions.

    start

    Is an integer that specifies where the substring begins.

    length

    Is an integer that specifies the length of the substring (the number of characters or bytes toreturn).

    Note Becausestartand length specify the number of bytes when SUBSTRING is used ontext data, DBCS data, such as Kanji, may result in split characters at the beginning or end ofthe result. This behavior is consistent with the way in which READTEXT handles DBCS;however, because of the occasional strange result, it is advisable to use ntext instead oftextfor DBCS characters

    SELECT substring('GAIT COMPUTER',2,3)

    Out Put AIT

    REPLACE (T-SQL)Replaces all occurrences of the second given string expression in the first string expression with a third expression.

    Syntax

    REPLACE('string_expression1', 'string_expression2', 'string_expression3')

    Arguments

    'string_expression1'

    Is the string expression to search forstring_expression2.string_expression1 can be ofcharacter or binary data.

    'string_expression2'

    Is the string expression for which to search instring_expression1 and to replace withstring_expression3.string_expression2 can be of character or binary data.

  • 8/3/2019 SQL Funcation 12 Class

    10/24

    'string_expression3'

    Is the new string expression that replacesstring_expression2instring_expression1.string_expression3 can be of character or binary data.

    Return Types

    Returns character data ifstring_expression (1, 2, or 3)is one of the supported character data types. Returns binarydataifstring_expression (1, 2, or 3)is one of the supported binary data types.

    Examples

    This example replaces the string cde in abcdefghi with xxx.SELECT REPLACE('abcdefghicde','cde','xxx')GO

    Here is the result set:------------------------------------------------------------------------abxxxfghixxx(1 row(s) affected)

    SELECT replace('GAIT COMPUTER','put','xxx')

    Output GAIT COMxxxER

    RTRIM (T-SQL)Returns a character string after removing all trailing blanks.

    Syntax

    RTRIM(character_expression)

    Arguments

    character_expression

    Is an expression of character data. character_expression can be a constant, variable, orcolumn of either character or binary data.

    Return Types

    varchar

    Remarks

    character_expressionmust be of a data type that is implicitly convertible to varchar. Otherwise, use the CASTfunction to explicitly convert character_expression.

    Note Compatibility levels can affect return values. For more information, see sp_dbcmptlevel.

    Examples

    This example shows using RTRIM to remove trailing spaces from a character variable.DECLARE @string_to_trim varchar(60)SET @string_to_trim = 'Four spaces are after the period in this sentence. 'SELECT 'Here is the string without the leading spaces: ' + CHAR(13) +RTRIM(@string_to_trim)GO

    Here is the result set:

    http://opt/scribd/conversion/tmp/scratch6270/sp_da-di_2.htmhttp://opt/scribd/conversion/tmp/scratch6270/sp_da-di_2.htmhttp://opt/scribd/conversion/tmp/scratch6270/sp_da-di_2.htm
  • 8/3/2019 SQL Funcation 12 Class

    11/24

    (1 row(s) affected)------------------------------------------------------------------------Here is the string without the leading spaces: Four spaces are after theperiod in this sentence.(1 row(s) affected)

    LTRIM (T-SQL)Returns a character expression after removing leading blanks.Syntax

    LTRIM(character_expression)

    Arguments

    character_expression

    Is an expression of character or binary data. character_expression can be a constant, variable or column.character_expression must be of a data type that is implicitly convertible to varchar. Otherwise, use CAST to

    explicitly convert character_expression.

    Return Type

    varchar

    Remarks

    Compatibility levels can affect return values. For more information about compatibility levels, see sp_dbcmptlevel.

    Examples

    This example uses LTRIM to remove leading spaces from a character variable.

    DECLARE @string_to_trim varchar(60)

    SET @string_to_trim = ' Five spaces are at the beginning of thisstring.'

    SELECT 'Here is the string without the leading spaces: ' +LTRIM(@string_to_trim)

    GO

    Here is the result set:

    ------------------------------------------------------------------------

    Here is the string without the leading spaces: Five spaces are at the beginning of this string.

    (1 row(s) affected)

    Returns a character string after removing all trailing blanks.Syntax

  • 8/3/2019 SQL Funcation 12 Class

    12/24

    RTRIM (T-SQL)

    RTRIM(character_expression)

    Arguments

    character_expression

    Is an expression of character data. character_expression can be a constant, variable, or column of either character orbinary data.

    Return Types

    varchar

    Remarks

    character_expression must be of a data type that is implicitly convertible to varchar. Otherwise, use the CASTfunction to explicitly convert character_expression.

    Note Compatibility levels can affect return values. For more information, see sp_dbcmptlevel.

    Examples

    This example shows using RTRIM to remove trailing spaces from a character variable.

    DECLARE @string_to_trim varchar(60)

    SET @string_to_trim = 'Four spaces are after the period in this sentence. '

    SELECT 'Here is the string without the leading spaces: ' + CHAR(13) +

    RTRIM(@string_to_trim)GO

    Here is the result set:

    (1 row(s) affected)

    ------------------------------------------------------------------------

    Here is the string without the leading spaces: Four spaces are after the period in this sentence.

    (1 row(s) affected)

  • 8/3/2019 SQL Funcation 12 Class

    13/24

    COUNT (T-SQL)Returns the number of items in a group.Syntax

    COUNT({[ALL | DISTINCT] expression] | *})

    Arguments

    ALL

    Applies the aggregate function to all values. ALL is the default.

    DISTINCT

    Specifies that COUNT returns the number of unique nonnull values.

    expression

    Is an expression of any type except uniqueidentifier, text, image, or ntext. Aggregate functions and subqueries are

    not permitted.

    *

    Specifies that all rows should be counted to return the total number of rows in a table. COUNT(*) takes noparameters and cannot be used with DISTINCT. COUNT(*) does not require an expression parameter because, bydefinition, it does not use information about any particular column. COUNT(*) returns the number of rows in aspecified table without eliminating duplicates. It counts each row separately, including rows that contain null values.

    Important Distinct aggregates, for example AVG(DISTINCT column_name), COUNT(DISTINCT column_name),MAX(DISTINCT column_name), MIN(DISTINCT column_name), and SUM(DISTINCT column_name), are notsupported when using CUBE or ROLLUP. If used, Microsoft SQL Server returns an error message and cancelsthe query.

    Return Types

    int

    Remarks

    COUNT(*) returns the number of items in a group, including NULL values and duplicates.COUNT(ALL expression) evaluates expression for each row in a group and returns the number of nonnull values.COUNT(DISTINCT expression) evaluates expression for each row in a group and returns the number of unique,nonnull values.

    ExamplesA. Use COUNT and DISTINCT

    This example finds the number of different cities in which authors live.

    USE pubs

    GO

    SELECT COUNT(DISTINCT city)

  • 8/3/2019 SQL Funcation 12 Class

    14/24

    FROM authors

    GO

    Here is the result set:

    -----------

    16

    (1 row(s) affected)

    B. Use COUNT(*)

    This example finds the total number of books and titles.

    USE pubs

    GO

    SELECT COUNT(*)

    FROM titles

    GO

    Here is the result set:

    -----------

    18

    (1 row(s) affected)

    C. Use COUNT(*) with other aggregates

    The example shows that COUNT(*) can be combined with other aggregate functions in the select list.

    USE pubs

    GO

    SELECT COUNT(*), AVG(price)

  • 8/3/2019 SQL Funcation 12 Class

    15/24

    FROM titles

    WHERE advance > $1000

    GO

    Here is the result set:

    ----------- --------------------------

    15 14.42

    (1 row(s) affected)

    SUM (T-SQL)Returns the sum of all the values, or only the DISTINCT values, in the expression. SUM can be used with numericcolumns only. Null values are ignored.Syntax

    SUM([ALL | DISTINCT] expression)

    Arguments

    ALL

    Applies the aggregate function to all values. ALL is the default.

    DISTINCT

    Specifies that SUM return the sum of unique values.

    expression

    Is a constant, column, or function, and any combination of arithmetic, bitwise, and string operators. expression is anexpression of the exact numeric or approximate numeric data type category, except for the bit data type. Aggregatefunctions and subqueries are not permitted.

    Return Types

    Returns the summation of all expression values in the most precise expression data type.

    Expression result Return typeinteger category intdecimal category (p, s) decimal(38, s)money and smallmoney category moneyfloat and real category float

  • 8/3/2019 SQL Funcation 12 Class

    16/24

    Important Distinct aggregates, for example AVG(DISTINCT column_name), COUNT(DISTINCT column_name),MAX(DISTINCT column_name), MIN(DISTINCT column_name), and SUM(DISTINCT column_name), are notsupported when using CUBE or ROLLUP. If used, Microsoft SQL Server returns an error message and cancelsthe query.

    ExamplesA. Use SUM for aggregates and row aggregates

    These examples show the differences between aggregate functions and row aggregate functions. The first showsaggregate functions giving summary data only, and the second shows row aggregate functions giving detail andsummary data.

    USE pubs

    GO

    -- Aggregate functions

    SELECT type, SUM(price), SUM(advance)

    FROM titles

    WHERE type LIKE '%cook'

    GROUP BY type

    ORDER BY type

    GO

    Here is the result set:

    type

    ------------ -------------------------- --------------------------

    mod_cook 22.98 15,000.00

    trad_cook 47.89 19,000.00

    (2 row(s) affected)

    USE pubs

    GO

    -- Row aggregates

  • 8/3/2019 SQL Funcation 12 Class

    17/24

    SELECT type, price, advance

    FROM titles

    WHERE type LIKE '%cook'

    ORDER BY type

    COMPUTE SUM(price), SUM(advance) BY type

    Here is the result set:

    type price advance

    ------------ -------------------------- --------------------------

    mod_cook 19.99 0.00

    mod_cook 2.99 15,000.00

    sum

    ==========================

    22.98

    sum

    ==========================

    15,000.00

    type price advance

    ------------ -------------------------- --------------------------

    trad_cook 20.95 7,000.00

    trad_cook 11.95 4,000.00

    trad_cook 14.99 8,000.00

    sum

    ==========================

    47.89

  • 8/3/2019 SQL Funcation 12 Class

    18/24

    sum

    ==========================

    19,000.00

    (7 row(s) affected)

    B. Calculate group totals with more than one column

    This example calculates the sum of the prices and advances for each type of book.

    USE pubs

    GO

    SELECT type, SUM(price), SUM(advance)

    FROM titles

    GROUP BY type

    ORDER BY type

    GO

    Here is the result set:

    type

    ------------ -------------------------- --------------------------

    business 54.92 25,125.00

    mod_cook 22.98 15,000.00

    popular_comp 42.95 15,000.00

    psychology 67.52 21,275.00

    trad_cook 47.89 19,000.00

    UNDECIDED (null) (null)

    (6 row(s) affected)

  • 8/3/2019 SQL Funcation 12 Class

    19/24

    AVG (T-SQL)Returns the average of the values in a group. Null values are ignored.Syntax

    AVG([ALL | DISTINCT] expression)

    Arguments

    ALL

    Applies the aggregate function to all values. ALL is the default.

    DISTINCT

    Specifies that AVG be performed only on each unique instance of a value, regardless of how many times the valueoccurs.

    expression

    Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.Aggregate functions and subqueries are not permitted.

    Return Types

    The return type is determined by the type of the evaluated result of expression.

    Expression result Return typeinteger category intdecimal category (p, s) decimal(38, s) divided by decimal(10, 0)money and smallmoney category money

    float and real category floatImportant Distinct aggregates, for example, AVG(DISTINCT column_name), COUNT(DISTINCT column_name),MAX(DISTINCT column_name), MIN(DISTINCT column_name), and SUM(DISTINCT column_name), are notsupported when using CUBE or ROLLUP. If used, Microsoft SQL Server returns an error message and cancelsthe query.

    ExamplesA. Use SUM and AVG functions for calculations

    This example calculates the average advance and the sum of year-to-date sales for all business books. Each of theseaggregate functions produces a single summary value for all of the retrieved rows.

    USE pubs

    SELECT AVG(advance), SUM(ytd_sales)

    FROM titles

    WHERE type = 'business'

  • 8/3/2019 SQL Funcation 12 Class

    20/24

    Here is the result set:

    -------------------------- -----------

    6,281.25 30788

    (1 row(s) affected)

    B. Use SUM and AVG functions with a GROUP BY clause

    When used with a GROUP BY clause, each aggregate function produces a single value for each group, rather thanfor the whole table. This example produces summary values for each type of book that include the average advancefor each type of book and the sum of year-to-date sales for each type of book.

    USE pubs

    SELECT type, AVG(advance), SUM(ytd_sales)

    FROM titles

    GROUP BY type

    ORDER BY type

    Here is the result set:

    type

    ------------ -------------------------- -----------

    business 6,281.25 30788

    mod_cook 7,500.00 24278

    popular_comp 7,500.00 12875

    psychology 4,255.00 9939

    trad_cook 6,333.33 19566

    UNDECIDED NULL NULL

    (6 row(s) affected)

  • 8/3/2019 SQL Funcation 12 Class

    21/24

    C. Use AVG with DISTINCT

    This statement returns the average of the distinct prices of business books.

    USE pubs

    SELECT AVG(DISTINCT price)

    FROM titles

    WHERE type = 'business'

    Here is the result set:

    --------------------------

    11.64

    (1 row(s) affected)

    D. Use AVG without DISTINCT

    Without DISTINCT, the AVG function finds the average price of all business titles in the titles table.

    USE pubs

    SELECT AVG(price)

    FROM titles

    WHERE type = 'business'

    Here is the result set:

    --------------------------

    13.73

    (1 row(s) affected)

    MIN (T-SQL)Returns the minimum value in the expression.Syntax

  • 8/3/2019 SQL Funcation 12 Class

    22/24

    MIN([ALL | DISTINCT] expression)

    Arguments

    ALL

    Applies the aggregate function to all values. ALL is the default.

    DISTINCT

    Specifies that each unique value is considered. DISTINCT is not meaningful with MIN and is available for SQL-92compatibility only.

    expression

    Is a constant, column name, or function, and any combination of arithmetic, bitwise, and string operators. MIN canbe used with numeric, char, varchar, or datetime columns, but not with bit columns. Aggregate functions andsubqueries are not permitted.

    Return Types

    Returns a value same as expression.

    Important Distinct aggregates, for example AVG(DISTINCT column_name), COUNT(DISTINCT column_name),MAX(DISTINCT column_name), MIN(DISTINCT column_name), and SUM(DISTINCT column_name), are notsupported when using CUBE or ROLLUP. If used, Microsoft SQL Server returns an error message and ends thequery.

    Remarks

    MIN ignores any null values.With character data columns, MIN finds the value that is lowest in the sort sequence.

    Examples

    This example returns the book with the lowest (minimum) year-to-date sales.

    USE pubs

    GO

    SELECT min(ytd_sales)

    FROM titles

    GO

    Here is the result set:

    -----------

    111

  • 8/3/2019 SQL Funcation 12 Class

    23/24

    (1 row(s) affected)

    MAX (T-SQL)Returns the maximum value in the expression.Syntax

    MAX([ALL | DISTINCT] expression)

    Arguments

    ALL

    Applies the aggregate function to all values. ALL is the default.

    DISTINCT

    Specifies that each unique value is considered. DISTINCT is not meaningful with MAX and is available for SQL-92compatibility only.

    expression

    Is a constant, column name, or function, and any combination of arithmetic, bitwise, and string operators. MAX canbe used with numeric, character, and datetime columns, but not with bit columns. Aggregate functions andsubqueries are not permitted.

    Return Types

    Returns a value same as expression.

    Important Distinct aggregates, for example AVG(DISTINCT column_name), COUNT(DISTINCT column_name),MAX(DISTINCT column_name), MIN(DISTINCT column_name), and SUM(DISTINCT column_name), are notsupported when using CUBE or ROLLUP. If used, Microsoft SQL Server returns an error message and cancels

    the query.

    Remarks

    MAX ignores any null values.For character columns, MAX finds the highest value in the collating sequence.

    Examples

    This example returns the book with the highest (maximum) year-to-date sales.

    USE pubs

    GO

    SELECT MAX(ytd_sales)

    FROM titles

    GO

  • 8/3/2019 SQL Funcation 12 Class

    24/24

    Here is the result set:

    -----------

    22246

    (1 row(s) affected)

    Warning, null value eliminated from aggregate.