2

我有一个这样的数据列表:(它在不同的城市继续存在)

Ref_Date    City           Province                   sex          age   Value
2010    St. John's   Newfoundland and Labrador  Males   15 to 19 years  2720
2010    St. John's   Newfoundland and Labrador  Males   20 to 24 years  5670
2010    St. John's   Newfoundland and Labrador  Males   25 to 29 years  5910
2010    St. John's   Newfoundland and Labrador  Males   30 to 34 years  5900
2010    St. John's   Newfoundland and Labrador  Males   35 to 39 years  6120
2010    St. John's   Newfoundland and Labrador  Males   40 to 44 years  6160
2010    St. John's   Newfoundland and Labrador  Males   45 to 49 years  6450
2010    St. John's   Newfoundland and Labrador  Males   50 to 54 years  5990
2010    St. John's   Newfoundland and Labrador  Males   55 to 59 years  5050
2010    St. John's   Newfoundland and Labrador  Males   60 to 64 years  3830
2010    St. John's   Newfoundland and Labrador  Males   65 years and over 2820  
2010    St. John's   Newfoundland and Labrador  Females 15 to 19 years  2840
2010    St. John's   Newfoundland and Labrador  Females 20 to 24 years  5820
2010    St. John's   Newfoundland and Labrador  Females 25 to 29 years  6080
2010    St. John's   Newfoundland and Labrador  Females 30 to 34 years  6070
2010    St. John's   Newfoundland and Labrador  Females 35 to 39 years  6120
2010    St. John's   Newfoundland and Labrador  Females 40 to 44 years  6450
2010    St. John's   Newfoundland and Labrador  Females 45 to 49 years  6750
2010    St. John's   Newfoundland and Labrador  Females 50 to 54 years  6090
2010    St. John's   Newfoundland and Labrador  Females 55 to 59 years  4990
2010    St. John's   Newfoundland and Labrador  Females 60 to 64 years  3410
2010    St. John's   Newfoundland and Labrador  Females 65 years and over 2320  

现在我想将每两行(但不考虑 65+ 组)与值的总和相结合并更改年龄组:如下所示:

Ref_Date    City         Province                   sex    age           Value
2010    St. John's   Newfoundland and Labrador  Males   15 to 24 years  2720+5670
2010    St. John's   Newfoundland and Labrador  Males   25 to 34 years  5910+5900
2010    St. John's   Newfoundland and Labrador  Males   35 to 44 years  6120+6160
2010    St. John's   Newfoundland and Labrador  Males   45 to 54 years  6450+5990
2010    St. John's   Newfoundland and Labrador  Males   55 to 64 years  5050+3830
2010    St. John's   Newfoundland and Labrador  Males   65 years and over 2820  

有没有办法做到这一点?我坚持了很长时间。

4

1 回答 1

1

You need to do an aggregation. This version defines the new age group in a subquery:

select ref_date, city, province, sex, newage, SUM(value) as value
from (select ref_date, city, province, sex, age, value,
             (case when age in ('15 to 19 years', '20 to 24 years')
                   then '15 to 24 years'
                   when age in ('25 to 29 years', '30 to 34 years')
                   then '25 to 34 years'
                   when age in ('35 to 39 years', '40 to 44 years')
                   then '35 to 44 years'
                   when age in ('45 to 49 years', '50 to 54 years')
                   then '45 to 54 years'
                   when age in ('55 to 59 years', '60 to 64 years')
                   then '55 to 64 years'
                   else age
               end) as newage
      from t
     ) t
group by ref_date, city, province, sex, newage
于 2013-03-21T21:46:27.827 回答