表的查询和数据之和
地类码净面积代码 名称
11125.166330227012001上何村
11416.898330227012001上何村
155k5.4029330227012001上何村
155k35.692330227012001上何村
1145.8239 330227012005张华山村
114 12.9330227012005张华山村
114 12.9330227012005张华山村
请问如果得到下面的表:
如果得到
名称 110 <地类码 <120 地类码=155k
上何村 净面积(25.166+16.898) 净面积(5.4029+35.692)
张华山村 净面积(5.8239+12.9+12.9) 净面积(0)
上面要得到净面积之和.
[解决办法]
declare @t table(地类码 varchar(10),净面积 float,代码 varchar(20),名称 varchar(20))
insert @t
select '111 ',25.166, '330227012001 ', '上何村 ' union all
select '114 ',16.898, '330227012001 ', '上何村 ' union all
select '155k ',5.4029, '330227012001 ', '上何村 ' union all
select '155k ',35.692, '330227012001 ', '上何村 ' union all
select '114 ',5.8239, '330227012005 ', '张华山村 ' union all
select '114 ', 12.9, '330227012005 ', '张华山村 ' union all
select '114 ', 12.9, '330227012005 ', '张华山村 '
select 名称,
[110 <地类码 <120] = isnull(sum(case when isnumeric(地类码)=1 and cast(地类码 as int) between 110 and 120 then 净面积 end),0),
[地类码=155k] = isnull(sum(case when 地类码= '155k ' then 净面积 end),0)
from @t group by 名称
/*结果
名称 110 <地类码 <120 地类码=155k
----------------------
上何村 42.064 41.094900000000003
张华山村 31.623899999999999 0.0
*/