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


  • 相关阅读:
    浅谈if __name__='__main__'
    python打开文件方式
    关于ValueError: not enough values to unpack (expected 4, got 1)
    一篇文章让你搞懂this
    Cannot read property 'addEventListener' of null报错的解决办法
    看JS操作符中二进制补码
    js的原型和原型链理解
    你不知道的js中对象,数组互相转换
    浅谈js中a+++a与a+a++值区别
    Integer跟int的区别
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2767085.html
Copyright © 2011-2022 走看看