• 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


  • 相关阅读:
    力扣(LeetCode)67. 二进制求和
    力扣(LeetCode) 66. 加一
    力扣(LeetCode)58. 最后一个单词的长度
    力扣(LeetCode)1009. 十进制整数的反码
    力扣(LeetCode) 20. 有效的括号
    力扣(LeetCode)1016. 子串能表示从 1 到 N 数字的二进制串
    数据库索引
    OSI 七层和五层
    ST算法
    F
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2767085.html
Copyright © 2020-2023  润新知