格式化数据透视表中的交叉表

时间:2012-03-06 03:42:16

标签: sql sql-server-2008 pivot-table

我有这个按年龄分组的种族

        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://img834.imageshack.us/img834/8808/sqlnow.jpg

但我需要的是这个,没有年龄范围。

1 http://img716.imageshack.us/img716/1462/sqllater.jpg

我将把一个数据透视表放在一个带有自定义标题的Radgrid中,该标题将显示年龄范围,因此列标题中不需要它们。这可能吗? 提前致谢

1 个答案:

答案 0 :(得分:0)

我不确定您是否希望将年龄范围折叠为单个列,或者您是否希望在查询中重复使用相同的列名称。如果你想让它们崩溃:

http://sqlfiddle.com/#!3/f0ca0/3

    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  
      P.city,
      P.sex,
      [20-30_African-American] + [31-40_African-American] + [41-50_African-American] as [African-American],

      [20-30_Asian] + [31-40_Asian] + [41-50_Asian] as [Asian],

      [20-30_Caucasian] + [31-40_Caucasian] + [41-50_Caucasian] as [Caucasian],

      [20-30_Hispanic] + [31-40_Hispanic] + [41-50_Hispanic] as [Hispanic]
   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

如果你真的想要多次返回相同的列名,我想你真的很头疼。这是我能提供的最接近的(虽然它不是一个很好的解决方案,因为它不会超出你的基础设置):

http://sqlfiddle.com/#!3/f0ca0/5

    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  
      P.city,
      P.sex,
          [20-30_African-American] as [African-American1], 
          [20-30_Asian] as [Asian1], 
          [20-30_Caucasian] as [Caucasian1], 
          [20-30_Hispanic] as [Hispanic1],
          [31-40_African-American] as [African-American2], 
          [31-40_Asian] as [Asian2], 
          [31-40_Caucasian] as [Caucasian2], 
          [31-40_Hispanic] as [Hispanic2],
          [41-50_African-American] as [African-American3], 
          [41-50_Asian] as [Asian3], 
          [41-50_Caucasian] as [Caucasian3], 
          [41-50_Hispanic] as [Hispanic3]

   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