如何从SQL存储过程中获取col结果的总和

How to do sum of col results from a SQL Stored Procedure

本问题已经有最佳答案,请猛点这里访问。

我有一个存储过程,结果如下:

1
2
3
4
5
6
7
Governors   AUTO    07313570121     1   3.69    2.01    2.01    1.68    83.58%
Governors   AUTO    07319354850     1   2.79    1.8     1.80    0.99    55.00%
Governors   AUTO    07480400400     1   17.69   9.71    9.7117  7.9783  82.15%
Governors   AUTO    07723100038     1   2.89    1.55    1.55    1.34    86.45%
Governors   BEER    01820000031     6   4.69    23.34   3.888   0.8     20.57%
Governors   BEER    01820000051     6   4.69    23.34   3.888   0.802   20.63%
Governors   BEER    01820000106     1   6.39    4.93    4.93    1.46    29.61%

我想总结如下:

1
2
 Governors   AUTO  4  27.06  15.07  
 Governors   AUTO  13 13.07  51.61


如果没有列名,这会有点棘手,但会有如下情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CREATE TABLE #MyTable
(
  col1 VARCHAR(50),
  col2 VARCHAR(4),
  col3 VARCHAR(11),
  col4 INT,
  col5 DECIMAL(18,2),
  col6 DECIMAL(18,2),
  col7 DECIMAL(18,2),
  col8 DECIMAL(18,2),
  col9 DECIMAL(18,2) --might need to be varchar if the % sign comes back
)

INSERT INTO #MyTable
EXEC storedProc

SELECT col1, col2, SUM(col4), SUM(col5), SUM(col7)
FROM #MyTable
GROUP BY col1, col2