Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 每两行合并一次,每七行除外(求和值,并更改年龄范围)_Sql_Sql Server 2008_Tsql - Fatal编程技术网

Sql 每两行合并一次,每七行除外(求和值,并更改年龄范围)

Sql 每两行合并一次,每七行除外(求和值,并更改年龄范围),sql,sql-server-2008,tsql,Sql,Sql Server 2008,Tsql,我有一个这样的数据列表:(它在不同的城市持续不断) 现在我想将每两行(但不包括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 N

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

现在我想将每两行(但不包括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  

有办法吗?我坚持了很长时间。

你需要做一个聚合。此版本在子查询中定义新的年龄组:

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

@dreamcrash thx用于编辑格式非常感谢。这很有效。顺便说一句,对一些很棒的SQL编程书籍有什么建议吗?
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