-2

我正在寻找旋转这些数据,以便输出可以计算每个年龄段的人数......

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

我正在使用 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 等...

4

1 回答 1

4

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

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

编辑,如果您想要男性/女性列,您将在 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

于 2013-04-05T15:38:07.677 回答