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
  • 相关阅读:
    MessageFormat理解,MessageFormat.format(Object obj)方法
    正则表达式
    数字处理类
    包装类
    遍历Map的4种方法(来自网络)
    集合类
    数组
    字符串
    语言基础
    Linux下使用openssl加解密
  • 原文地址:https://www.cnblogs.com/goodspeed/p/1077817.html
Copyright © 2011-2022 走看看