SQL 2012 Group By Rollup, Grouping

Microshaoft發表於2014-02-13

GO
alter proc [zsp_BranchsData]
as
begin
/*
    CREATE TABLE [原始機構資料]
    (
        [序號]                [varchar](50) NULL,
        [一級分行號]            [varchar](50) NULL,
        [一級分行名稱]        [varchar](50) NULL,
        [二級分行號]            [varchar](50) NULL,
        [二級分行名稱]        [varchar](50) NULL,
        --[上級機構號]        [varchar](50) NULL,
        --[上級機構名稱]        [varchar](50) NULL,
        --[責任中心號]        [varchar](50) NULL,
        --[責任中心名稱]        [varchar](50) NULL
    )
*/
    SELECT
        --GROUPING([一級分行號])
        --, 
        [一級分行號]        =
                            iif
                                (
                                    GROUPING([一級分行號]) = 1
                                    , '合計:'
                                    , [一級分行號]
                                ) ,
        [一級分行名稱]    =
                            iif
                                (
                                    GROUPING([一級分行號]) = 1
                                    ,
                                        cast
                                            (
                                                count(distinct [一級分行號]) as varchar
                                            )
                                    , max([一級分行名稱])
                                ) , 
        [二級分行數量]    = count(distinct [二級分行號])
    FROM
        [原始機構資料]
    group by
        --[一級分行號]
        rollup
            (
                [一級分行號]
            )
    order by
        GROUPING([一級分行號])
        , [一級分行號]
    select
        [二級分行號] ,
        [二級分行名稱]    = max([二級分行名稱]) ,
        [一級分行號]        = max([一級分行號]) ,
        [一級分行名稱]    = max([一級分行名稱])
    from
        [原始機構資料]
    group by
        [二級分行號]
    order by
        3
;with T
as
(
    SELECT
        [一級分行組]            = GROUPING([一級分行號]) ,
        [全轄一級分行序號]    = DENSE_RANK()
                                over
                                    (
                                        order by
                                            [一級分行號]
                                    ) - 1 ,
        [一級分行號]            =
                                iif
                                    (
                                        GROUPING([一級分行號]) = 1
                                        , '全部合計:'
                                        , [一級分行號]
                                    ) ,
        [一級分行名稱]        =
                                case
                                    when
                                        GROUPING([一級分行號]) = 1
                                            then
                                                cast(count(distinct [一級分行號]) as varchar)
                                    when
                                        GROUPING([二級分行號]) = 1
                                        and GROUPING([一級分行號]) = 0
                                            then
                                                max([一級分行名稱]) + ' 分組'
                                    else
                                        max([一級分行名稱])
                                end ,
        [二級分行組]            = GROUPING([二級分行號]) ,
        [轄內二級分行序號]    = ROW_NUMBER()
                                    over
                                        (
                                            partition by
                                                [一級分行號]
                                            order by
                                                [二級分行號]
                                        ) - 1 ,
        [二級分行號]            =
                                case
                                    when
                                        GROUPING([一級分行號]) = 1
                                        and GROUPING([二級分行號]) = 1
                                            then
                                                '全部合計:'
                                    when
                                        GROUPING([二級分行號]) = 1
                                            then
                                                '分組小計:'
                                    else
                                        [二級分行號]
                                end ,
        [二級分行名稱]        =
                                iif
                                    (
                                        GROUPING([二級分行號]) = 1
                                        , cast(count(distinct [二級分行號]) as varchar)
                                        , max([二級分行名稱])
                                    )
    FROM
        [原始機構資料]
    group by
        --[一級分行號]
        rollup
            (
                [一級分行號]
                , [二級分行號]
            )
)
select
    --[一級分行組] ,
    [全轄一級分行序號] ,
    [一級分行號] ,
    [一級分行名稱] ,
    --[二級分行組] ,
    [轄內二級分行序號] ,
    [二級分行號] ,
    [二級分行名稱]
from
    T
order by
    [一級分行號] ,
    [一級分行組] ,
    [二級分行號] ,
    [二級分行組]
end

相關文章