您好,登錄后才能下訂單哦!
這篇文章主要介紹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多維數據集的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。