有人可以为我打破PIVOT查询吗?

时间:2013-04-05 15:31:48

标签: sql sql-server

我希望调整这些数据,以便输出可以计算每个年龄段的人数......

以下是我所做的,但我甚至不认为我很接近......

我正在使用SQL Server 2008

SELECT     [OA_Code], Dominant_F_Age, Age_F_90plus, Age_F_85_89, Age_F_80_84, Age_F_75_79, Age_F_70_74, Age_F_65_69, Age_F_60_64, 
                      Age_F_55_59, Age_F_50_54, Age_F_45_49, Age_F_40_44, Age_F_35_39, Age_F_30_34, Age_F_25_29, Age_F_20_24, Age_F_15_19, Age_F_10_14, Age_F_5_9, 
                      Age_F_0_4, Age_2001_F
FROM         cen.AgeByGenderOA
PIVOT (
    f_Age_data
    for [F_Age_Data] in (Age_F_90plus, Age_F_85_89, Age_F_80_84, Age_F_75_79, Age_F_70_74, Age_F_65_69, Age_F_60_64, 
                      Age_F_55_59, Age_F_50_54, Age_F_45_49, Age_F_40_44, Age_F_35_39, Age_F_30_34, Age_F_25_29, Age_F_20_24, Age_F_15_19, Age_F_10_14, Age_F_5_9), 
                      Age_F_0_4)

我的表看起来像这样:

CREATE TABLE AgeByGenderOA(
            [OA_Code] AS VARCHAR(50),
            [Age_M_0_4] [varchar](50) NULL,
        [Age_M_5_9] [varchar](50) NULL,
        [Age_M_10_14] [varchar](50) NULL,
        [Age_M_15_19] [varchar](50) NULL,
        [Age_M_20_24] [varchar](50) NULL,
        [Age_M_25_29] [varchar](50) NULL,
        [Age_M_30_34] [varchar](50) NULL,
        [Age_M_35_39] [varchar](50) NULL,
        [Age_M_40_44] [varchar](50) NULL,
        [Age_M_45_49] [varchar](50) NULL,
        [Age_M_50_54] [varchar](50) NULL,
        [Age_M_55_59] [varchar](50) NULL,
        [Age_M_60_64] [varchar](50) NULL,
        [Age_M_65_69] [varchar](50) NULL,
        [Age_M_70_74] [varchar](50) NULL,
        [Age_M_75_79] [varchar](50) NULL,
        [Age_M_80_84] [varchar](50) NULL,
        [Age_M_85_89] [varchar](50) NULL,
        [Age_M_90plus] [varchar](50) NULL,
        [Dominant_M_Age] [varchar](50) NULL,
        [Age_2001_F] [varchar](50) NULL,
        [Age_F_0_4] [varchar](50) NULL,
        [Age_F_5_9] [varchar](50) NULL,
        [Age_F_10_14] [varchar](50) NULL,
        [Age_F_15_19] [varchar](50) NULL,
        [Age_F_20_24] [varchar](50) NULL,
        [Age_F_25_29] [varchar](50) NULL,
        [Age_F_30_34] [varchar](50) NULL,
        [Age_F_35_39] [varchar](50) NULL,
        [Age_F_40_44] [varchar](50) NULL,
        [Age_F_45_49] [varchar](50) NULL,
        [Age_F_50_54] [varchar](50) NULL,
        [Age_F_55_59] [varchar](50) NULL,
        [Age_F_60_64] [varchar](50) NULL,
        [Age_F_65_69] [varchar](50) NULL,
        [Age_F_70_74] [varchar](50) NULL,
        [Age_F_75_79] [varchar](50) NULL,
        [Age_F_80_84] [varchar](50) NULL,
        [Age_F_85_89] [varchar](50) NULL,
        [Age_F_90plus] [varchar](50) NULL,
        [Dominant_F_Age] [varchar](50) NULL,
        [MAPINFO_ID] [varchar](50) NULL)

你可以看到它是一张巨大的桌子。 “Age_M_0_4”列填充了数字。

我希望我的输出读取

OA_Code, Age, countOfAge
123456, Age_0_4, 26
123456, Age_5_9, 24
789456, Age_0_4, 10
789456, Age_5_9, 12

这将包括男性和女性年龄。

有人喜欢在这个问题上采取行动?

最后:“Age_M_”列中包含的数据都是数字计数。 50,40,2,0等...

1 个答案:

答案 0 :(得分:4)

你不需要PIVOT,你需要UNPIVOT数据。由于您使用的是SQL Server 2008+,因此可以将CROSS APPLYVALUES子句一起使用。代码将类似于:

select t.OA_Code,
  c.age,
  sum(cast(c.value as int)) countOfAge
from AgeByGenderOA t
cross apply
(
  values 
    ('Age_0_4', Age_M_0_4),
    ('Age_5_9', Age_M_5_9),
    ('Age_10_14', Age_M_10_14),
    ('Age_15_19', Age_M_15_19) ... add the other columns here
) c (age, value)
group by t.OA_Code, c.age

请参阅SQL Fiddle with Demo

编辑,如果你想要两个男性/女性列,你将在values子句中包含所有列,但是给它们相同的年龄范围值:

select t.OA_Code,
  c.age,
  sum(cast(c.value as int)) countOfAge
from AgeByGenderOA t
cross apply
(
  values 
    ('Age_0_4', Age_M_0_4),
    ('Age_5_9', Age_M_5_9),
    ('Age_10_14', Age_M_10_14),
    ('Age_15_19', Age_M_15_19),
    ('Age_0_4', Age_F_0_4),
    ('Age_5_9', Age_F_5_9),
    ('Age_10_14', Age_F_10_14),
    ('Age_15_19', Age_F_15_19)
) c (age, value)
group by t.OA_Code, c.age;

请参阅SQL Fiddle with Demo