SQLserver中cube:多维数据集实例详解
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复制删除发布时遇到错误18752的问题及解决方法
朋友反馈他无法删除一台SQL Server数据库上的发布,具体情况为删除一个SQL Server Replication的发布时,遇到这样的错误问题如何解决呢,下面小编给大家分享SQL Server复制删除发布时遇到错误18752的问题及解决方法,感兴趣的朋友一起看看吧2024-01-01分页存储过程(二)在sqlserver中返回更加准确的分页结果
分页存储过程(二)在MS SQL Server中返回更加准确的分页结果2010-05-05Sql server 2012 中文企业版安装图文教程(附下载链接)
这篇文章主要介绍了Sql server 2012 中文企业版安装图文教程(附下载链接),需要的朋友可以参考下2020-04-04Windows Server2008 R2 MVC 环境安装配置教程
这篇文章主要为大家详细介绍了Windows Server2008 R2 MVC 环境安装配置教程,感兴趣的小伙伴们可以参考一下2016-09-09针对Sqlserver大数据量插入速度慢或丢失数据的解决方法
这篇文章主要介绍了针对Sqlserver大数据量插入速度慢或丢失数据的解决方法,很有实用价值,需要的朋友可以参考下2014-07-07
最新评论