zoukankan      html  css  js  c++  java
  • SQL SERVER 2008的Grouping sets的用法

    CREATE TABLE Sales (EmpId INT, Yr INT, Sales MONEY)

    INSERT Sales VALUES(1, 2005, 12000)

    INSERT Sales VALUES(1, 2006, 18000)

    INSERT Sales VALUES(1, 2007, 25000)

    INSERT Sales VALUES(2, 2005, 15000)

    INSERT Sales VALUES(2, 2006, 6000)

    INSERT Sales VALUES(3, 2006, 20000)

    INSERT Sales VALUES(3, 2007, 24000)

    SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY EmpId, Yr WITH ROLLUP

    SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY EmpId, Yr WITH CUBE

    SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), (EmpId), ())

    EmpId       Yr          Sales
    ----------- ----------- ---------------------
    1           2005        12000.00
    1           2006        18000.00
    1           2007        25000.00
    1           NULL        55000.00
    2           2005        15000.00
    2           2006        6000.00
    2           NULL        21000.00
    3           2006        20000.00
    3           2007        24000.00
    3           NULL        44000.00
    NULL        NULL        120000.00

    SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), (EmpId), (Yr), ())

    EmpId       Yr          Sales
    ----------- ----------- ---------------------
    1           2005        12000.00
    2           2005        15000.00
    NULL        2005        27000.00
    1           2006        18000.00
    2           2006        6000.00
    3           2006        20000.00
    NULL        2006        44000.00
    1           2007        25000.00
    3           2007        24000.00
    NULL        2007        49000.00
    NULL        NULL        120000.00
    1           NULL        55000.00
    2           NULL        21000.00
    3           NULL        44000.00


    SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), (EmpId))

    EmpId       Yr          Sales
    ----------- ----------- ---------------------
    1           2005        12000.00
    1           2006        18000.00
    1           2007        25000.00
    1           NULL        55000.00
    2           2005        15000.00
    2           2006        6000.00
    2           NULL        21000.00
    3           2006        20000.00
    3           2007        24000.00
    3           NULL        44000.00

    SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr), ())

    EmpId       Yr          Sales
    ----------- ----------- ---------------------
    1           2005        12000.00
    1           2006        18000.00
    1           2007        25000.00
    2           2005        15000.00
    2           2006        6000.00
    3           2006        20000.00
    3           2007        24000.00
    NULL        NULL        120000.00

    SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId), (Yr))

    EmpId       Yr          Sales
    ----------- ----------- ---------------------
    NULL        2005        27000.00
    NULL        2006        44000.00
    NULL        2007        49000.00
    1           NULL        55000.00
    2           NULL        21000.00
    3           NULL        44000.00

    SELECT EmpId, Yr, SUM(Sales) AS Sales FROM Sales GROUP BY GROUPING SETS((EmpId, Yr))

    EmpId       Yr          Sales
    ----------- ----------- ---------------------
    1           2005        12000.00
    2           2005        15000.00
    1           2006        18000.00
    2           2006        6000.00
    3           2006        20000.00
    1           2007        25000.00
    3           2007        24000.00


  • 相关阅读:
    51nod 237 最大公约数之和 V3 杜教筛
    luogu P4213 【模板】杜教筛(Sum)
    BZOJ 3527: [Zjoi2014]力 FFT
    凸多边形 HRBUST
    luogu P1354 房间最短路问题 计算几何_Floyd_线段交
    几何基础
    BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 Hash + Splay
    BZOJ3529: [Sdoi2014]数表 莫比乌斯反演_树状数组
    BZOJ 2820: YY的GCD 莫比乌斯反演 + 数学推导 + 线性筛
    迭代器,三元表达式,列表生成式,字典生成式,生成器,递归(没深入理解)
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2767085.html
Copyright © 2011-2022 走看看