香港云主机最佳企业级服务商!

ADSL拨号VPS包含了中国大陆(联通,移动,电信,)

中国香港,国外拨号VPS。

当前位置:云主机 > MSSQL >

电信ADSL拨号VPS
联通ADSL拨号VPS
移动ADSL拨号VPS

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


时间:2020-10-31 14:32 作者:admin


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

根据需要使用union all 拼接

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

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

举例:

SELECT * INTO ##GETFROM   (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 ##GETGROUP 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/' target='_blank'>mysql子查询和嵌套查询优化实例解析、几个比较重要的MySQL变量、ORACLE SQL语句优化技术要点解析等,有什么问题可以随时留言,小编会及时回复大家的。感谢各位对本站的支持!

(责任编辑:admin)






帮助中心
会员注册
找回密码
新闻中心
快捷通道
域名登录面板
虚机登录面板
云主机登录面板
关于我们
关于我们
联系我们
联系方式

售前咨询:17830004266(重庆移动)

企业QQ:383546523

《中华人民共和国工业和信息化部》 编号:ICP备00012341号

Copyright © 2002 -2018 香港云主机 版权所有
声明:香港云主机品牌标志、品牌吉祥物均已注册商标,版权所有,窃用必究

云官方微信

在线客服

  • 企业QQ: 点击这里给我发消息
  • 技术支持:383546523

  • 公司总台电话:17830004266(重庆移动)
  • 售前咨询热线:17830004266(重庆移动)