新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍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多维数据集的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!