主頁 > 知識(shí)庫 > SQLserver中cube:多維數(shù)據(jù)集實(shí)例詳解

SQLserver中cube:多維數(shù)據(jù)集實(shí)例詳解

熱門標(biāo)簽:百度地圖標(biāo)注不同路線 智能語音外呼系統(tǒng)選哪家 千呼電銷機(jī)器人價(jià)格 京華物流公司地圖標(biāo)注 武漢長沙外呼系統(tǒng)方法和技巧 怎樣在地圖上標(biāo)注路線圖標(biāo) 外呼系統(tǒng)電銷專用 優(yōu)質(zhì)地圖標(biāo)注 奧威地圖標(biāo)注多個(gè)地方

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
			[店名]

總結(jié)

以上就是本文關(guān)于SQLserver中cube:多維數(shù)據(jù)集實(shí)例詳解的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱:MYSQL子查詢和嵌套查詢優(yōu)化實(shí)例解析、幾個(gè)比較重要的MySQL變量、ORACLE SQL語句優(yōu)化技術(shù)要點(diǎn)解析等,有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。感謝各位對(duì)本站的支持!

您可能感興趣的文章:
  • SQL Server 2008還原數(shù)據(jù)庫的方法
  • Python3操作SQL Server數(shù)據(jù)庫(實(shí)例講解)
  • C#實(shí)現(xiàn)連接SQL Server2012數(shù)據(jù)庫并執(zhí)行SQL語句的方法
  • sql server2008數(shù)據(jù)庫遷移的兩種方法
  • SQL SERVER 2008數(shù)據(jù)庫日志文件收縮的方法
  • Windows Server 2016 MySQL數(shù)據(jù)庫安裝配置詳細(xì)安裝教程
  • Python基于Pymssql模塊實(shí)現(xiàn)連接SQL Server數(shù)據(jù)庫的方法詳解
  • C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫中的方法
  • SQL Server 遠(yuǎn)程更新目標(biāo)表數(shù)據(jù)的存儲(chǔ)過程

標(biāo)簽:銅仁 七臺(tái)河 威海 益陽 天水 防疫戰(zhàn)設(shè) 來賓 宿州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQLserver中cube:多維數(shù)據(jù)集實(shí)例詳解》,本文關(guān)鍵詞  SQLserver,中,cube,多維,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQLserver中cube:多維數(shù)據(jù)集實(shí)例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于SQLserver中cube:多維數(shù)據(jù)集實(shí)例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章