built-in functions - aggregate functions - sqlserverpedia

Upload: sagalog

Post on 14-Apr-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    1/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    1/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    Built-

    in

    Func

    -

    Aggr

    Func

    See

    Also:

    Main_

    -

    TransaSQL

    Coding

    Techni

    -

    Reusa

    Coding

    Techni

    -

    Functio

    -

    SQL

    Server

    2005

    Built-

    Ins

    Aggreg

    functio

    return

    a

    single

    value

    summ

    a

    given

    data

    set.

    All

    aggreg

    functio

    are

    determ

    Search SQLServerPedi

    Wiki Pulse Blog Project Lucy Podcasts Twitter Contributors Log in

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    2/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    2/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    NOTE:

    AVG,

    SUM,

    STDEV

    STDEV

    VAR

    and

    VARP

    functio

    cannot

    operat

    on

    BIT

    data

    types;

    they

    canoperat

    on

    all

    other

    numeri

    data

    types.

    Con

    [hide]

    1

    2

    3

    4

    5

    6

    7

    8

    9

    1

    1

    1

    1

    1

    COUFun

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    3/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    3/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    The

    COUN

    functio

    returns

    a

    count

    of

    rows

    based

    on

    certain

    criteria.

    The

    syntax

    is :

    Keywor

    ALL

    is

    optiona

    and

    is

    assum

    by

    default.

    If

    you

    specify

    a

    "*"

    asthe

    criterio

    COUN

    returns

    the

    total

    numbe

    of

    rows

    in

    a

    table;

    for

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    4/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    4/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    exampl

    the

    followi

    query

    counts

    rows

    in

    the

    FactFin

    table

    of

    Advent

    Works

    DW

    databa

    If

    you

    join

    multipl

    tables

    then

    COUN

    returns

    the

    numbe

    of

    rows

    satisfyi

    the

    join

    criterio

    as

    in

    the

    followi

    1.

    2.

    3.

    4.

    5.

    6.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    5/49

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    6/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    6/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    ignore

    NULL

    values.

    If

    you

    need

    to

    find

    the

    count

    of

    unique

    items

    within

    a

    column

    in

    a

    table

    use

    COUN

    (DISTI

    column

    syntax.

    For

    examplthe

    followi

    query

    counts

    unique

    organi

    keys

    within

    the

    FactFin

    table:

    1.

    2.

    3.

    4.

    5.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    7/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    7/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    COUFun

    The

    COUN

    functio

    is

    identic

    to

    the

    COUN

    functio

    but

    returns

    aBIGINT

    data

    type,

    wherea

    COUN

    returns

    an

    INT.

    The

    upper

    limit

    for

    INT

    data

    type

    is

    2

    billion;

    therefo

    if

    you

    anticip

    countin

    more

    than

    two

    billion

    rows

    use

    the

    COUN

    6.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    8/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    8/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    functio

    Any

    attemp

    to

    use

    the

    COUN

    functio

    when

    countin

    over

    2

    billion

    rows

    returns

    an

    error.

    MAXFun

    The

    MAX

    functio

    returns

    the

    bigges

    value

    within

    a

    given

    set.

    The

    syntax

    is :

    The

    ALL

    keywor

    is

    optiona

    and

    is

    the

    default

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    9/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    9/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    unless

    DISTIN

    is

    specifi

    The

    DISTIN

    keywor

    specifi

    that

    each

    unique

    value

    should

    be

    consid

    This

    keyworreally

    has

    no

    use

    with

    MAX

    functio

    since

    it

    returns

    a

    single

    value;

    howev

    the

    keywor

    is

    suppor

    for

    ANSI

    compa

    For

    exampl

    the

    followi

    returnsthe

    greate

    amoun

    from

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    10/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    10/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    the

    FactFin

    table:

    Note

    that

    the

    param

    of

    MAX

    can

    be

    any

    valid

    expres

    includi

    string

    column

    as

    in

    the

    followi

    "Wome

    Tights,

    S"

    is

    1.

    2.

    3.

    4.

    5.

    6.

    1.

    2.

    3.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    11/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    11/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    returne

    becaus

    it is

    the

    last

    value

    in

    the

    alphab

    ordere

    list

    of

    produc

    names

    not

    becaus

    it isthe

    longes

    produc

    name.

    You

    can

    combin

    MAX

    functio

    with

    LEN

    functio

    to

    return

    the

    produc

    with

    the

    longes

    names

    as

    follows

    01.

    02.

    03.

    04.

    05.

    06.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    12/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    12/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    07.

    08.

    09.

    10.

    11.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    13/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    13/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    MINFun

    The

    MIN

    functio

    returns

    the

    smalle

    value

    within

    a

    given

    set.

    The

    syntax

    is :

    The

    ALL

    keywor

    is

    optionaand

    is

    the

    default

    unless

    DISTIN

    is

    specifi

    The

    DISTIN

    keywor

    specifi

    that

    each

    unique

    value

    should

    be

    consid

    This

    keywor

    really

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    14/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    14/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    has

    no

    use

    with

    MIN

    functio

    since

    it

    returns

    a

    single

    value;

    howev

    the

    keywor

    is

    supporfor

    ANSI

    compa

    For

    exampl

    the

    followi

    returnsthe

    least

    amoun

    from

    the

    FactFin

    table:

    1.

    2.

    3.

    4.

    5.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    15/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    15/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    Note

    that

    the

    param

    of

    MIN

    can

    be

    any

    valid

    expres

    includistring

    column

    as

    in

    the

    followi

    6.

    1.

    2.

    3.

    4.

    5.

    6.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    16/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    16/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    Note

    that

    "Adjust

    Race"

    is

    returne

    becaus

    it is

    the

    first

    value

    in

    thealphab

    ordere

    list

    of

    produc

    names

    not

    becaus

    it is

    the

    shorte

    produc

    name.

    You

    can

    combin

    MIN

    functio

    with

    LEN

    functio

    to

    7.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    17/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    17/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    return

    the

    produc

    with

    the

    shorte

    names

    as

    follows

    01.

    02.

    03.

    04.

    05.

    06.

    07.

    08.

    09.

    10.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    18/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    18/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    AVGFun

    The

    AVG

    functio

    returns

    the

    averag

    of

    the

    values

    withina

    column

    Unlike

    MIN

    and

    MAX,

    AVG

    can

    only

    accept

    a

    numeri

    expres

    as

    a

    param

    The

    syntax

    is :

    ALL

    keywor

    is

    optiona

    and

    is

    assum

    11.

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    19/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    19/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    by

    default.

    You

    can

    specify

    the

    DISTIN

    keywor

    if

    you

    want

    to

    take

    into

    accoun

    only

    theunique

    occurre

    of

    each

    value.

    The

    followi

    query

    returnsthe

    averag

    amoun

    from

    FactFin

    table:

    The

    next

    exampl

    1.

    2.

    3.

    4.

    5.

    6.

    7.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    20/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    20/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    uses

    DISTIN

    keywor

    to

    return

    the

    averag

    of

    distinct

    values:

    The

    AVG

    functio

    returns

    the

    same

    (or

    a

    similar

    data

    type

    as

    the

    group

    of

    values

    it

    accept

    So

    if

    youpass

    an

    integer

    expect

    1.

    2.

    3.

    4.

    5.

    6.

    7.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    21/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    21/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    an

    integer

    back;

    if

    you

    examin

    float

    values

    with

    the

    AVG

    functio

    you

    will

    get

    a

    floatvalue

    back.

    SUMFun

    The

    SUMfunctio

    returns

    the

    sum

    of

    all

    or

    unique

    values.

    Unlike

    MIN

    and

    MAX,

    SUM

    can

    only

    accept

    a

    numeri

    expres

    as

    a

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    22/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    22/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    param

    The

    syntax

    is :

    ALL

    keywor

    is

    optiona

    and

    is

    assum

    by

    default.

    You

    can

    specify

    the

    DISTIN

    keywor

    if

    you

    want

    to

    take

    into

    accoun

    only

    the

    unique

    occurreof

    each

    value.

    The

    followi

    query

    returns

    the

    sum

    of

    amoun

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    23/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    23/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    from

    FactFin

    table:

    The

    next

    exampl

    uses

    DISTIN

    keywor

    to

    return

    the

    sumof

    distinct

    values:

    The

    SUM

    functio

    returns

    the

    1.

    2.

    3.

    4.

    5.

    6.

    7.

    1.

    2.

    3.

    4.

    5.

    6.

    7.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    24/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    24/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    same

    (or

    a

    similar

    data

    type

    as

    the

    group

    of

    values

    it

    accept

    So

    if

    you

    passan

    integer

    expect

    an

    integer

    back;

    if

    you

    examin

    float

    values

    with

    the

    SUM

    functio

    you

    will

    get

    a

    float

    value

    back.

    STDFun

    The

    STDEV

    functio

    calcula

    the

    standa

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    25/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    25/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    deviati

    for

    all

    items

    in

    the

    SELEC

    statem

    This

    functio

    can

    only

    be

    used

    with

    numeri

    columnThe

    syntax

    is :

    ALL

    keywor

    is

    optiona

    and

    is

    assum

    by

    default.

    Youcan

    specify

    the

    DISTIN

    keywor

    if

    you

    want

    to

    take

    into

    accoun

    only

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    26/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    26/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    the

    unique

    occurre

    of

    each

    value.

    The

    STDEV

    functio

    always

    returns

    a

    FLOAT

    data

    type

    value.

    The

    followi

    exampl

    returns

    the

    standa

    deviati

    of

    amoun

    from

    FactFin

    table:

    1.

    2.

    3.

    4.

    5.

    6.

    7.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    27/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    27/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    STDFun

    The

    STDEV

    functiocalcula

    the

    standa

    deviati

    for

    the

    popula

    of

    items

    in

    the

    SELEC

    statem

    This

    functio

    can

    only

    be

    used

    with

    numeri

    column

    The

    syntax

    is :

    ALL

    keywor

    is

    optiona

    and

    is

    assum

    by

    default.

    You

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    28/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    28/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    can

    specify

    the

    DISTIN

    keywor

    if

    you

    want

    to

    take

    into

    accoun

    only

    the

    unique

    occurre

    ofeach

    value.

    The

    STDEV

    functio

    always

    returns

    a

    FLOAT

    data

    type

    value.

    The

    followi

    exampl

    returns

    the

    standa

    deviati

    of

    popula

    for

    amoun

    in

    the

    FactFintable:

    1.

    2.

    3.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    29/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    29/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    VARFun

    The

    VAR

    functio

    calcula

    the

    statisti

    varianc

    of

    all

    values

    in

    the

    SELEC

    statem

    This

    functio

    can

    only

    be

    used

    with

    numeri

    column

    The

    syntax

    is :

    4.

    5.

    6.

    7.

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    30/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    30/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    ALL

    keywor

    is

    optiona

    and

    is

    assum

    by

    default.

    You

    can

    specify

    the

    DISTINkeywor

    if

    you

    want

    to

    take

    into

    accoun

    only

    the

    unique

    occurre

    of

    each

    value.

    The

    VAR

    functio

    always

    returns

    a

    FLOAT

    data

    type

    value.

    Thefollowi

    exampl

    returns

    the

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    31/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    31/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    varianc

    of

    amoun

    from

    FactFin

    table:

    VARFun

    The

    VARP

    functio

    calcula

    the

    statisti

    variancfor

    the

    popula

    of

    values

    in

    the

    SELEC

    statem

    This

    functio

    can

    only

    1.

    2.

    3.

    4.

    5.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    32/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    32/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    be

    used

    with

    numeri

    column

    The

    syntax

    is :

    ALL

    keywor

    is

    optiona

    and

    is

    assum

    by

    default.

    You

    can

    specify

    the

    DISTIN

    keywor

    if

    you

    want

    to

    take

    into

    accounonly

    the

    unique

    occurre

    of

    each

    value.

    The

    VARP

    functio

    always

    returns

    a

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    33/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    33/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    FLOAT

    data

    type

    value.

    The

    followi

    exampl

    returns

    the

    varianc

    for

    the

    popula

    of

    amounfrom

    the

    FactFin

    table:

    CHEFun

    The

    CHEC

    functio

    can

    be

    1.

    2.

    3.

    4.

    5.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    34/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    34/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    used

    to

    detect

    data

    change

    in

    a

    table.

    This

    functio

    can

    only

    work

    on

    integer

    data

    type;otherwi

    the

    functio

    is

    very

    similar

    to

    other

    aggreg

    functio

    like

    SUM

    or

    AVG.

    The

    syntax

    is :

    ALL

    keywor

    is

    optiona

    and

    is

    assum

    by

    default.

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    35/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    35/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    You

    can

    specify

    the

    DISTIN

    keywor

    if

    you

    want

    to

    take

    into

    accoun

    only

    the

    unique

    occurreof

    each

    value.

    The

    CHEC

    functio

    always

    returns

    a

    INT

    data

    type

    value.

    The

    followi

    query

    returns

    the

    checks

    of

    amoun

    column

    in

    FactFin

    table;

    thenit

    update

    some

    rows

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    36/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    36/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    in

    the

    table;

    the

    second

    checks

    is

    differen

    from

    the

    first

    indicati

    that

    the

    data

    values

    havechange

    Result

    1.

    2.

    3.

    1.

    2.

    3.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    37/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    37/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    CHEFun

    The

    CHEC

    functio

    returns

    the

    checks

    value

    compu

    over

    a

    row

    in

    a

    given

    table;

    alterna

    it

    can

    return

    the

    checks

    of

    the

    specifi

    list

    of

    values.

    This

    functio

    isintende

    for

    buildin

    hash

    4.

    5.

    6.

    7.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    38/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    38/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    indexe

    and

    always

    returns

    an

    INT

    data

    type

    value.

    The

    syntax

    is :

    The

    CHEC

    flavor

    examin

    all

    column

    of

    a

    given

    table;

    it

    returns

    an

    error

    if

    the

    table

    contain

    TEXT,

    NTEXT

    or

    IMAGE

    data

    type

    column

    Ifyou

    specify

    expres

    instead

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    39/49

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    40/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    40/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    The

    next

    exampl

    returns

    a

    CHEC

    of

    given

    values:

    To

    add

    a

    hash

    index

    to

    a

    table

    you

    need

    to

    add

    a

    compu

    column

    using

    CHEC

    5.

    1.

    2.

    3.

    4.

    5.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    41/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    41/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    functio

    and

    then

    create

    an

    index

    on

    the

    compu

    column

    as

    in

    the

    followi

    GR

    FunThe

    GROU

    functio

    returns

    a

    value

    of

    1 if

    the

    row

    is

    added

    to

    the

    result

    set

    by

    eitherROLLU

    or

    CUBE

    clause

    1.

    2.

    3.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    42/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    42/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    of

    the

    SELEC

    statem

    CUBE

    and

    ROLLU

    extensi

    allow

    genera

    very

    simple

    textual

    reports

    in

    a

    querywindo

    Majorit

    of

    busine

    reports

    will

    use

    more

    sophis

    reporti

    tools

    than

    the

    Query

    Analyze

    or

    SQL

    Server

    Manag

    Studio,

    therefo

    CUBE

    and

    ROLLU

    extensi

    as

    well

    asthe

    GROU

    functio

    have

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    43/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    43/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    limited

    usage.

    The

    syntax

    of

    the

    GROU

    functio

    is :

    The

    column

    is

    a

    column

    referre

    to

    in

    the

    GROU

    BY

    statem

    The

    followi

    query

    returns

    sum

    of

    amoun

    per

    organiand

    total

    of

    all

    organi

    amoun

    combin

    as

    a

    separa

    "groupi

    row":

    1.

    01.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    44/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    44/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    02.

    03.04.

    05.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    45/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    45/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    06.

    07.

    08.

    09.

    10.

    11.

    12.

    13.

    14.

    15.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    46/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    46/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    The

    ROLLU

    extensi

    and

    the

    GROU

    functio

    are

    more

    useful

    when

    the

    report

    is

    groupi

    recordbased

    on

    multipl

    column

    For

    exampl

    the

    followi

    query

    groups

    the

    output

    by

    produc

    class,

    style

    and

    Englis

    name:

    1.

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    47/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    47/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    Note

    that

    a

    groupi

    row

    is

    genera

    for

    each

    class,

    each

    class

    and

    style

    combin

    and

    the

    grand

    total

    of

    all

    sales.

    MorSQL

    ServFun

    S

    S

    2

    S

    F

    -

    li

    L

    R

    C

    S

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    48/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    48/49qlserv erpedia.com/wiki/Built-in_Functions_-_Aggregate_Functions

    S

    S

    2

    C

    F

    -

    li

    C

    S

    S

    2

    D

    &

    Ti

    F

    -li

    D

    D

    G

    S

    S

    2

    M

    F

    -

    li

    R

    C

    F

    S

    S

    2

    T

    a

    I

    F

    -

    li

    P

    T

    S

    S

    2

    S

    F

    -

  • 7/30/2019 Built-In Functions - Aggregate Functions - SQLServerPedia

    49/49

    9/03/13 Built-in F unctions - Aggregate F unctions - SQLServe rPedia

    Powered by MediaWiki | 2012 Dell Inc., ALL RIGHTS RESERVED. | Privacy Policy | Terms of Use | Contact

    Us | Follow Us on Twitter

    li

    C

    I

    E

    S

    S

    2

    C

    F

    -

    li

    S

    S

    2

    F-

    if

    y

    c

    o

    d

    a

    S

    S

    2

    th

    p

    c

    th

    li

    o

    fu

    s

    b

    2