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  

次に、2行ごとに(ただし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

集計を行う必要があります。このバージョンは、サブクエリで新しい年齢層を定義します。

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 に答える