主页 > 知识库 > SQLserver中cube:多维数据集实例详解

SQLserver中cube:多维数据集实例详解

热门标签:百度竞价排名 网站排名优化 AI电销 服务外包 Linux服务器 地方门户网站 呼叫中心市场需求 铁路电话系统

1、cube:生成多维数据集,包含各维度可能组合的交叉表格,使用with 关键字连接 with cube

根据需要使用union all 拼接

判断 某一列的null值来自源数据还是 cube 使用GROUPING关键字

GROUPING([档案号]) = 1 : null值来自cube(代表所有的档案号)
GROUPING([档案号]) = 0 : null值来自源数据

举例:

SELECT * INTO ##GET
FROM 
  (SELECT *
    FROM ( SELECT
      CASE
      WHEN (GROUPING([档案号]) = 1) THEN
      '合计'
      ELSE [档案号]
      END AS '档案号',
      CASE
      WHEN (GROUPING([系列]) = 1) THEN
      '合计'
      ELSE [系列]
      END AS '系列',
      CASE
      WHEN (GROUPING([店长]) = 1) THEN
      '合计'
      ELSE [店长]
      END AS '店长', SUM (剩余次数) AS '总剩余',
      CASE
      WHEN (GROUPING([店名]) = 1) THEN
      '合计'
      ELSE [店名]
      END AS '店名'
    FROM ##PudianCard
    GROUP BY [档案号], [店名], [店长], [系列]
    WITH cube
    HAVING GROUPING([店名]) != 1
        AND GROUPING([档案号]) = 1 --AND GROUPING([系列]) = 1 ) AS M
    UNION
    ALL 
      (SELECT *
        FROM ( SELECT
          CASE
          WHEN (GROUPING([档案号]) = 1) THEN
          '合计'
          ELSE [档案号]
          END AS '档案号',
          CASE
          WHEN (GROUPING([系列]) = 1) THEN
          '合计'
          ELSE [系列]
          END AS '系列',
          CASE
          WHEN (GROUPING([店长]) = 1) THEN
          '合计'
          ELSE [店长]
          END AS '店长', SUM (剩余次数) AS '总剩余',
          CASE
          WHEN (GROUPING([店名]) = 1) THEN
          '合计'
          ELSE [店名]
          END AS '店名'
        FROM ##PudianCard
        GROUP BY [档案号], [店名], [店长], [系列]
        WITH cube
        HAVING GROUPING([店名]) != 1
            AND GROUPING([店长]) != 1 ) AS P )
        UNION
        ALL 
          (SELECT *
            FROM ( SELECT
              CASE
              WHEN (GROUPING([档案号]) = 1) THEN
              '合计'
              ELSE [档案号]
              END AS '档案号',
              CASE
              WHEN (GROUPING([系列]) = 1) THEN
              '合计'
              ELSE [系列]
              END AS '系列',
              CASE
              WHEN (GROUPING([店长]) = 1) THEN
              '合计'
              ELSE [店长]
              END AS '店长', SUM (剩余次数) AS '总剩余',
              CASE
              WHEN (GROUPING([店名]) = 1) THEN
              '合计'
              ELSE [店名]
              END AS '店名'
            FROM ##PudianCard
            GROUP BY [档案号], [店名], [店长], [系列]
            WITH cube
            HAVING GROUPING([店名]) != 1
                AND GROUPING([店长]) != 1 ) AS W )
            UNION
            ALL 
              (SELECT *
                FROM ( SELECT
                  CASE
                  WHEN (GROUPING([档案号]) = 1) THEN
                  '合计'
                  ELSE [档案号]
                  END AS '档案号',
                  CASE
                  WHEN (GROUPING([系列]) = 1) THEN
                  '合计'
                  ELSE [系列]
                  END AS '系列',
                  CASE
                  WHEN (GROUPING([店长]) = 1) THEN
                  '合计'
                  ELSE [店长]
                  END AS '店长', SUM (剩余次数) AS '总剩余',
                  CASE
                  WHEN (GROUPING([店名]) = 1) THEN
                  '合计'
                  ELSE [店名]
                  END AS '店名'
                FROM ##PudianCard
                GROUP BY [档案号], [店名], [店长], [系列]
                WITH cube
                HAVING GROUPING([店名]) = 1
                    AND GROUPING([店长]) = 1
                    AND GROUPING([档案号]) = 1 ) AS K ) ) AS T

2、rollup:功能跟cube相似

3、将某一列的数据作为列名,动态加载,使用存储过程,拼接字符串

DECLARE @st nvarchar (MAX) = '';SELECT @st =@st + 'max(case when [系列]=''' + CAST ([系列] AS VARCHAR) + ''' then [总剩余] else null end ) as [' + CAST ([系列] AS VARCHAR) + '],'
FROM ##GET
GROUP BY [系列]; print @st;

4、根据某一列分组,分别建表

SELECT
				'select ROW_NUMBER() over(order by [卡项] desc) as [序号], [会员],[档案号],[卡项],[剩余次数],[员工],[店名] into ' + ltrim([店名]) + ' from 查询 where [店名]=''' + [店名] + ''' ORDER BY [卡项] desc'
		FROM
			查询
		GROUP BY
			[店名]

总结

以上就是本文关于SQLserver中cube:多维数据集实例详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅:MYSQL子查询和嵌套查询优化实例解析、几个比较重要的MySQL变量、ORACLE SQL语句优化技术要点解析等,有什么问题可以随时留言,小编会及时回复大家的。感谢各位对本站的支持!

您可能感兴趣的文章:
  • SQL Server 2008还原数据库的方法
  • Python3操作SQL Server数据库(实例讲解)
  • C#实现连接SQL Server2012数据库并执行SQL语句的方法
  • sql server2008数据库迁移的两种方法
  • SQL SERVER 2008数据库日志文件收缩的方法
  • Windows Server 2016 MySQL数据库安装配置详细安装教程
  • Python基于Pymssql模块实现连接SQL Server数据库的方法详解
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • SQL Server 远程更新目标表数据的存储过程

标签:湘潭 铜川 湖南 衡水 崇左 兰州 仙桃 黄山

巨人网络通讯声明:本文标题《SQLserver中cube:多维数据集实例详解》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266