1、cube:生成多維數據集,包含各維度可能組合的交叉表格,使用with 關鍵字連接 with cube 根據需要使用union all 拼接 判斷 某一列的null值來自源數據還是 cube 使用GROUPING關鍵字 GROUPING([檔案號]) = 1 : null值來自cube(代表所有的 ...
1、cube:生成多維數據集,包含各維度可能組合的交叉表格,使用with 關鍵字連接 with cube
根據需要使用union all 拼接
判斷 某一列的null值來自源數據還是 cube 使用GROUPING關鍵字
GROUPING([檔案號]) = 1 : null值來自cube(代表所有的檔案號)
GROUPING([檔案號]) = 0 : null值來自源數據
舉例:
1 SELECT * INTO ##GET 3 FROM 4 ( 5 SELECT 6 * 7 FROM 8 ( 9 SELECT 10 CASE 11 WHEN (GROUPING([檔案號]) = 1) THEN 12 '合計' 13 ELSE 14 [檔案號] 15 END AS '檔案號', 16 CASE 17 WHEN (GROUPING([系列]) = 1) THEN 18 '合計' 19 ELSE 20 [系列] 21 END AS '系列', 22 CASE 23 WHEN (GROUPING([店長]) = 1) THEN 24 '合計' 25 ELSE 26 [店長] 27 END AS '店長', 28 SUM (剩餘次數) AS '總剩餘', 29 CASE 30 WHEN (GROUPING([店名]) = 1) THEN 31 '合計' 32 ELSE 33 [店名] 34 END AS '店名' 35 FROM 36 ##PudianCard 37 GROUP BY 38 [檔案號], 39 [店名], 40 [店長], 41 [系列] WITH cube 42 HAVING 43 GROUPING([店名]) != 1 44 AND GROUPING([檔案號]) = 1 --AND GROUPING([系列]) = 1 45 ) AS M 46 UNION ALL 47 ( 48 SELECT 49 * 50 FROM 51 ( 52 SELECT 53 CASE 54 WHEN (GROUPING([檔案號]) = 1) THEN 55 '合計' 56 ELSE 57 [檔案號] 58 END AS '檔案號', 59 CASE 60 WHEN (GROUPING([系列]) = 1) THEN 61 '合計' 62 ELSE 63 [系列] 64 END AS '系列', 65 CASE 66 WHEN (GROUPING([店長]) = 1) THEN 67 '合計' 68 ELSE 69 [店長] 70 END AS '店長', 71 SUM (剩餘次數) AS '總剩餘', 72 CASE 73 WHEN (GROUPING([店名]) = 1) THEN 74 '合計' 75 ELSE 76 [店名] 77 END AS '店名' 78 FROM 79 ##PudianCard 80 GROUP BY 81 [檔案號], 82 [店名], 83 [店長], 84 [系列] WITH cube 85 HAVING 86 GROUPING([店名]) != 1 87 AND GROUPING([店長]) != 1 88 ) AS P 89 ) 90 UNION ALL 91 ( 92 SELECT 93 * 94 FROM 95 ( 96 SELECT 97 CASE 98 WHEN (GROUPING([檔案號]) = 1) THEN 99 '合計' 100 ELSE 101 [檔案號] 102 END AS '檔案號', 103 CASE 104 WHEN (GROUPING([系列]) = 1) THEN 105 '合計' 106 ELSE 107 [系列] 108 END AS '系列', 109 CASE 110 WHEN (GROUPING([店長]) = 1) THEN 111 '合計' 112 ELSE 113 [店長] 114 END AS '店長', 115 SUM (剩餘次數) AS '總剩餘', 116 CASE 117 WHEN (GROUPING([店名]) = 1) THEN 118 '合計' 119 ELSE 120 [店名] 121 END AS '店名' 122 FROM 123 ##PudianCard 124 GROUP BY 125 [檔案號], 126 [店名], 127 [店長], 128 [系列] WITH cube 129 HAVING 130 GROUPING([店名]) != 1 131 AND GROUPING([店長]) != 1 132 ) AS W 133 ) 134 UNION ALL 135 ( 136 SELECT 137 * 138 FROM 139 ( 140 SELECT 141 CASE 142 WHEN (GROUPING([檔案號]) = 1) THEN 143 '合計' 144 ELSE 145 [檔案號] 146 END AS '檔案號', 147 CASE 148 WHEN (GROUPING([系列]) = 1) THEN 149 '合計' 150 ELSE 151 [系列] 152 END AS '系列', 153 CASE 154 WHEN (GROUPING([店長]) = 1) THEN 155 '合計' 156 ELSE 157 [店長] 158 END AS '店長', 159 SUM (剩餘次數) AS '總剩餘', 160 CASE 161 WHEN (GROUPING([店名]) = 1) THEN 162 '合計' 163 ELSE 164 [店名] 165 END AS '店名' 166 FROM 167 ##PudianCard 168 GROUP BY 169 [檔案號], 170 [店名], 171 [店長], 172 [系列] WITH cube 173 HAVING 174 GROUPING([店名]) = 1 175 AND GROUPING([店長]) = 1 176 AND GROUPING([檔案號]) = 1 177 ) AS K 178 ) 179 ) 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
[店名]