zoukankan      html  css  js  c++  java
  • SQL Server 2008中的GROUPING SETS的应用实例

    DECLARE @t TABLE(student nvarchar(50),class nvarchar(50),grade int)

    /*insert some datas*/
    INSERT INTO @t
    SELECT N'孙小美',N'数学',10 UNION ALL
    SELECT N'孙小美',N'语文',20 UNION ALL
    SELECT N'孙小美',N'英语',30 UNION ALL
    SELECT N'阿土伯',N'数学',40 UNION ALL
    SELECT N'阿土伯',N'语文',50 UNION ALL
    SELECT N'阿土伯',N'英语',60 UNION ALL
    SELECT N'小叮铛',N'数学',70 UNION ALL
    SELECT N'小叮铛',N'语文',80 UNION ALL
    SELECT N'小叮铛',N'英语',90

    SELECT * FROM
    (
        
    SELECT 
            
    ISNULL(student,'ALL'AS student, 
            
    ISNULL(class,'ALL'AS class, 
            
    SUM(grade) AS grade
        
    FROM @t
        
    GROUP BY GROUPING SETS((student,class),student,class,())
    AS t
    PIVOT
    (
        
    MAX(grade) FOR class IN (数学,语文,英语,[ALL])
    AS p
    ORDER BY student DESC
  • 相关阅读:
    【bzoj2006】超级钢琴
    【bzoj4940】这是我自己的发明
    【arc076E】Connected?
    【agc004C】AND Grid
    选举
    几何
    打击目标
    【CF Gym100228】Graph of Inversions
    【CodeChef】Chef and Graph Queries
    大包子玩游戏
  • 原文地址:https://www.cnblogs.com/goodspeed/p/1077817.html
Copyright © 2011-2022 走看看