這篇文章主要介紹SQLserver中cube多維數(shù)據(jù)集的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
“專業(yè)、務(wù)實(shí)、高效、創(chuàng)新、把客戶的事當(dāng)成自己的事”是我們每一個(gè)人一直以來堅(jiān)持追求的企業(yè)文化。 成都創(chuàng)新互聯(lián)是您可以信賴的網(wǎng)站建設(shè)服務(wù)商、專業(yè)的互聯(lián)網(wǎng)服務(wù)提供商! 專注于網(wǎng)站制作、網(wǎng)站建設(shè)、軟件開發(fā)、設(shè)計(jì)服務(wù)業(yè)務(wù)。我們始終堅(jiān)持以客戶需求為導(dǎo)向,結(jié)合用戶體驗(yàn)與視覺傳達(dá),提供有針對(duì)性的項(xiàng)目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場,引領(lǐng)市場!
1、cube:生成多維數(shù)據(jù)集,包含各維度可能組合的交叉表格,使用with 關(guān)鍵字連接 with cube
根據(jù)需要使用union all 拼接
判斷 某一列的null值來自源數(shù)據(jù)還是 cube 使用GROUPING關(guān)鍵字
GROUPING([檔案號(hào)]) = 1 : null值來自cube(代表所有的檔案號(hào))
GROUPING([檔案號(hào)]) = 0 : null值來自源數(shù)據(jù)
舉例:
SELECT * INTO ##GET FROM (SELECT * FROM ( SELECT CASE WHEN (GROUPING([檔案號(hào)]) = 1) THEN '合計(jì)' ELSE [檔案號(hào)] END AS '檔案號(hào)', CASE WHEN (GROUPING([系列]) = 1) THEN '合計(jì)' ELSE [系列] END AS '系列', CASE WHEN (GROUPING([店長]) = 1) THEN '合計(jì)' ELSE [店長] END AS '店長', SUM (剩余次數(shù)) AS '總剩余', CASE WHEN (GROUPING([店名]) = 1) THEN '合計(jì)' ELSE [店名] END AS '店名' FROM ##PudianCard GROUP BY [檔案號(hào)], [店名], [店長], [系列] WITH cube HAVING GROUPING([店名]) != 1 AND GROUPING([檔案號(hào)]) = 1 --AND GROUPING([系列]) = 1 ) AS M UNION ALL (SELECT * FROM ( SELECT CASE WHEN (GROUPING([檔案號(hào)]) = 1) THEN '合計(jì)' ELSE [檔案號(hào)] END AS '檔案號(hào)', CASE WHEN (GROUPING([系列]) = 1) THEN '合計(jì)' ELSE [系列] END AS '系列', CASE WHEN (GROUPING([店長]) = 1) THEN '合計(jì)' ELSE [店長] END AS '店長', SUM (剩余次數(shù)) AS '總剩余', CASE WHEN (GROUPING([店名]) = 1) THEN '合計(jì)' ELSE [店名] END AS '店名' FROM ##PudianCard GROUP BY [檔案號(hào)], [店名], [店長], [系列] WITH cube HAVING GROUPING([店名]) != 1 AND GROUPING([店長]) != 1 ) AS P ) UNION ALL (SELECT * FROM ( SELECT CASE WHEN (GROUPING([檔案號(hào)]) = 1) THEN '合計(jì)' ELSE [檔案號(hào)] END AS '檔案號(hào)', CASE WHEN (GROUPING([系列]) = 1) THEN '合計(jì)' ELSE [系列] END AS '系列', CASE WHEN (GROUPING([店長]) = 1) THEN '合計(jì)' ELSE [店長] END AS '店長', SUM (剩余次數(shù)) AS '總剩余', CASE WHEN (GROUPING([店名]) = 1) THEN '合計(jì)' ELSE [店名] END AS '店名' FROM ##PudianCard GROUP BY [檔案號(hào)], [店名], [店長], [系列] WITH cube HAVING GROUPING([店名]) != 1 AND GROUPING([店長]) != 1 ) AS W ) UNION ALL (SELECT * FROM ( SELECT CASE WHEN (GROUPING([檔案號(hào)]) = 1) THEN '合計(jì)' ELSE [檔案號(hào)] END AS '檔案號(hào)', CASE WHEN (GROUPING([系列]) = 1) THEN '合計(jì)' ELSE [系列] END AS '系列', CASE WHEN (GROUPING([店長]) = 1) THEN '合計(jì)' ELSE [店長] END AS '店長', SUM (剩余次數(shù)) AS '總剩余', CASE WHEN (GROUPING([店名]) = 1) THEN '合計(jì)' ELSE [店名] END AS '店名' FROM ##PudianCard GROUP BY [檔案號(hào)], [店名], [店長], [系列] WITH cube HAVING GROUPING([店名]) = 1 AND GROUPING([店長]) = 1 AND GROUPING([檔案號(hào)]) = 1 ) AS K ) ) AS T
2、rollup:功能跟cube相似
3、將某一列的數(shù)據(jù)作為列名,動(dòng)態(tài)加載,使用存儲(chǔ)過程,拼接字符串
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、根據(jù)某一列分組,分別建表
SELECT 'select ROW_NUMBER() over(order by [卡項(xiàng)] desc) as [序號(hào)], [會(huì)員],[檔案號(hào)],[卡項(xiàng)],[剩余次數(shù)],[員工],[店名] into ' + ltrim([店名]) + ' from 查詢 where [店名]=''' + [店名] + ''' ORDER BY [卡項(xiàng)] desc' FROM 查詢 GROUP BY [店名]
以上是“SQLserver中cube多維數(shù)據(jù)集的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!