无为清净楼资源网 Design By www.qnjia.com
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语句优化技术要点解析等,有什么问题可以随时留言,小编会及时回复大家的。感谢各位对本站的支持!
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 永恒英文金曲精选4《TheBestOfEverlastingFavouritesVol.4》[WAV+CUE]
- 群星《国风超有戏 第9期》[320K/MP3][13.63MB]
- 群星《国风超有戏 第9期》[FLAC/分轨][72.56MB]
- P1X3L《P1X3L》[320K/MP3][143MB]
- 群星.2022-良辰好景知几何电视剧原声带【SONY】【FLAC分轨】
- 庾澄庆.1991-管不住自己【福茂】【WAV+CUE】
- 欧倩怡.2001-Cindy(EP)【华星】【WAV+CUE】
- P1X3L《P1X3L》[FLAC/分轨][791.28MB]
- 茵达INDAHKUS《茵 the Blue》[320K/MP3][33.49MB]
- 茵达INDAHKUS《茵 the Blue》[FLAC/分轨][147.57MB]
- 老虎鱼AnneClarkwithUllavanDaelenJustinCiuche-Borderland-HiRes-Files24bit88.2kHz
- 江玲2004-百合盛开2CD[歌林][WAV+CUE]
- 孙露《怕什么孤单3CD》深圳音像[WAV分轨]
- s14全球总决赛目前是什么结果 s14全球总决赛赛程结果图一览
- s2TPA夺冠五人名单都有谁 tpa战队s2夺冠队员名单一览