db2的rollup和cube函数

WBOY
發布: 2016-06-07 16:02:52
原創
1615 人瀏覽過

rollup的效果就是对group by后面的一个分组列名进行统计。 bankid为第一列 SELECT CASE WHEN grouping (branchid) = 0 THEN branchid ELSE 小计 END as branchid, CASE WHEN grouping (bankid) = 0 THEN bankid ELSE 总计 END as bankid, SUM (balance)FROM

rollup的效果就是对group by后面的一个分组列名进行统计。

bankid为第一列

SELECT
    CASE
        WHEN grouping (branchid) = 0
        THEN branchid
        ELSE '小计'
    END as branchid,
    CASE
        WHEN grouping (bankid) = 0
        THEN bankid
        ELSE '总计'
    END as bankid,
    SUM (balance)
FROM
    ebanksumm
WHERE
    workdate = '20140320'
GROUP BY
 bankid ,
        branchid
  with  rollup
登入後複製

branchid为第一列

SELECT
    CASE
        WHEN grouping (branchid) = 0
        THEN branchid
        ELSE '小计'
    END AS branchid ,
    CASE
        WHEN grouping (bankid) = 0
        THEN bankid
        ELSE '总计'
    END AS bankid ,
    SUM (balance)
FROM
    ebanksumm
WHERE
    workdate = '20140321'
GROUP BY
    branchid ,
    bankid WITH rollup
登入後複製

cube函数则是对groupby后面的所有列依次进行统计。和rollup的区别就在于要统计的列。rollup只统计一列,cube统计所有列

SELECT
    CASE
        WHEN grouping (branchid) = 0
        THEN branchid
        ELSE '小计'
    END as branchid,
    CASE
        WHEN grouping (bankid) = 0
        THEN bankid
        ELSE '总计'
    END as bankid,
    SUM (balance)
FROM
    ebanksumm
WHERE
    workdate = '20140321'
GROUP BY
 bankid ,
        branchid
  with  Cube
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!