2012-03-07 149 views
3

在我試圖行和列總計添加到該數據透視表SQL數據透視表總計

 create table test4(city nvarchar(10), race nvarchar(30), sex nvarchar(10), age int) 
      insert into test4 values ('Austin', 'African-American', 'male', 21) 
      insert into test4 values ('Austin', 'Asian', 'female', 22) 
      insert into test4 values ('Austin', 'Caucasian', 'male', 23) 
      insert into test4 values ('Austin', 'Hispanic', 'female', 24) 
      insert into test4 values ('Austin', 'African-American', 'Unknown', 25) 
      insert into test4 values ('Austin', 'Asian', 'male', 26) 
      insert into test4 values ('Austin', 'Caucasian', 'female', 27) 
      insert into test4 values ('Austin', 'Hispanic', 'Unknown', 28) 
      insert into test4 values ('Austin', 'Asian', 'male', 29) 
      insert into test4 values ('Austin', 'Caucasian', 'female', 31) 
      insert into test4 values ('Dallas', 'Hispanic', 'Unknown', 32) 
      insert into test4 values ('Dallas', 'African-American', 'male', 33) 
      insert into test4 values ('Dallas', 'Asian', 'female', 34) 
      insert into test4 values ('Dallas', 'Caucasian', 'Unknown', 35) 
      insert into test4 values ('Dallas', 'Hispanic', 'male', 500) 
      insert into test4 values ('Dallas', 'African-American', 'female', 36) 
      insert into test4 values ('Dallas', 'Asian', 'Unknown', 37) 
      insert into test4 values ('Dallas', 'Caucasian', 'male', 38) 
      insert into test4 values ('Dallas', 'Hispanic', 'female', 39) 
      insert into test4 values ('Dallas', 'African-American', 'Unknown', 41) 
      insert into test4 values ('Houston', 'Asian', 'male', 42) 
      insert into test4 values ('Houston', 'Caucasian', 'female', 43) 
      insert into test4 values ('Houston', 'Hispanic', 'Unknown', 44) 
      insert into test4 values ('Houston', 'African-American', 'male', 45) 
      insert into test4 values ('Houston', 'Asian', 'female', 46) 
      insert into test4 values ('Houston', 'Caucasian', 'Unknown', 47) 
      insert into test4 values ('Houston', 'Hispanic', 'male', 48) 
      insert into test4 values ('Houston', 'African-American', 'female', 49) 
      insert into test4 values ('Houston', 'Asian', 'Unknown', 51) 
      insert into test4 values ('Houston', 'Caucasian', 'male', 52); 

      WITH T AS (
      SELECT 
        A.city as city, A.sex as sex, 
        CASE 
         WHEN A.age BETWEEN 20 AND 30 THEN '20-30_' + race 
         WHEN A.age BETWEEN 31 AND 40 THEN '31-40_' + race 
         WHEN A.age BETWEEN 41 AND 50 THEN '41-50_' + race 
        END AS age_range_race 
      FROM test4 AS A 
      ) 
      SELECT * 
      FROM T 
      PIVOT(COUNT(age_range_race) FOR age_range_race 
       IN(
        [20-30_African-American], 
        [20-30_Asian], 
        [20-30_Caucasian], 
        [20-30_Hispanic], 
        [31-40_African-American], 
        [31-40_Asian], 
        [31-40_Caucasian], 
        [31-40_Hispanic], 
        [41-50_African-American], 
        [41-50_Asian], 
        [41-50_Caucasian], 
        [41-50_Hispanic] 
       ) 
      ) AS P 

我想實現這個結果

1 http://img853.imageshack.us/img853/8112/sqltotal.jpg

唯一的例子我可以在Google上找到爲SQL 2000編寫的存儲過程。請幫忙!在此先感謝

+1

你的腳本正在工作,對嗎? – 2012-03-07 03:25:52

+1

是的,我只是想創造總數。 – user973671 2012-03-07 03:28:28

+1

[你可以小計數據透視表中的行和/或列嗎?](http://stackoverflow.com/questions/7925748/can-you-subtotal-rows-and-or-columns-in-a- pivot-table) – bummi 2014-12-01 09:31:37

回答

8

在這裏,你試試。

WITH T AS (
      SELECT 
        A.city as city, A.sex as sex, 
        CASE 
         WHEN A.age BETWEEN 20 AND 30 THEN '20-30_' + race 
         WHEN A.age BETWEEN 31 AND 40 THEN '31-40_' + race 
         WHEN A.age BETWEEN 41 AND 50 THEN '41-50_' + race 
        END AS age_range_race 
      FROM #test4 AS A 
      ) 


SELECT *, ([20-30_African-American] + [20-30_Asian] + [20-30_Caucasian]+ [20-30_Hispanic]+ [31-40_African-American]+ [31-40_Asian]+ [31-40_Caucasian]+ [31-40_Hispanic]+[41-50_African-American]+ [41-50_Asian]+ [41-50_Caucasian]+ [41-50_Hispanic]) Total 
into #tmp_result 
FROM T 
PIVOT(COUNT(age_range_race) FOR age_range_race 
    IN(
     [20-30_African-American], 
     [20-30_Asian], 
     [20-30_Caucasian], 
     [20-30_Hispanic], 
     [31-40_African-American], 
     [31-40_Asian], 
     [31-40_Caucasian], 
     [31-40_Hispanic], 
     [41-50_African-American], 
     [41-50_Asian], 
     [41-50_Caucasian], 
     [41-50_Hispanic] 
    ) 
) AS P 

select * 
from #tmp_result 
union all 
select 'Grand Total','',SUM([20-30_African-American]), SUM([20-30_Asian]), SUM([20-30_Caucasian]), SUM([20-30_Hispanic]),SUM([31-40_African-American]), SUM([31-40_Asian]), SUM([31-40_Caucasian]), SUM([31-40_Hispanic]),SUM([41-50_African-American]), SUM([41-50_Asian]), SUM([41-50_Caucasian]),SUM([41-50_Hispanic]), sum(Total) 
from #tmp_result 
+1

這很完美,非常感謝。 – user973671 2012-03-07 15:02:01

+1

其真的很簡單,但我們可以修改它的參數嗎?能夠與動態列一起使用。 – Rapunzo 2013-03-06 09:33:00

2

請嘗試如下..

WITH T AS (
      SELECT 
        A.city as city, A.sex as sex, 
        CASE 
         WHEN A.age BETWEEN 20 AND 30 THEN '20-30_' + race 
         WHEN A.age BETWEEN 31 AND 40 THEN '31-40_' + race 
         WHEN A.age BETWEEN 41 AND 50 THEN '41-50_' + race 
        END AS age_range_race 
      FROM #test4 AS A 
      ) 


SELECT *, ([20-30_African-American] + [20-30_Asian] + [20-30_Caucasian]+ [20-30_Hispanic]+ [31-40_African-American]+ [31-40_Asian]+ [31-40_Caucasian]+ [31-40_Hispanic]+[41-50_African-American]+ [41-50_Asian]+ [41-50_Caucasian]+ [41-50_Hispanic]) Total 
FROM T 
PIVOT(COUNT(age_range_race) FOR age_range_race 
    IN(
     [20-30_African-American], 
     [20-30_Asian], 
     [20-30_Caucasian], 
     [20-30_Hispanic], 
     [31-40_African-American], 
     [31-40_Asian], 
     [31-40_Caucasian], 
     [31-40_Hispanic], 
     [41-50_African-American], 
     [41-50_Asian], 
     [41-50_Caucasian], 
     [41-50_Hispanic] 
    ) 
) AS P 
+1

這很好,但如果可能的話,我還需要底部的列,謝謝 – user973671 2012-03-07 03:42:19

+1

然後您需要將這些結果保存在臨時表中。之後,make是聯合腳本..選擇'Total','',sum([20-30_African-American])等 – 2012-03-07 03:48:31

+1

你能提供一個例子嗎?對不起,我完全不理解它。謝謝 – user973671 2012-03-07 03:57:35