日期:2014-05-18  浏览次数:20460 次

统计取值问题?
有如下表:
tb_test
c_No             c_Area         i_Value
123456         wh                 20              
123456         cs                 40
123456         gz                 60
111111         gz                 100
222222         wh                 60
222222         gz                 20

想通过分组统计得到
c_Area             i_sum             i_Count
wh                     60                   1
gz                     160                 2

就是以i_Value的最大值为准(i_Value在哪个c_Area最大,就归入哪个c_Area),按c_Area分组
第一,求归入c_Area的总i_Value
第二,统计此c_Area有多少个c_No

请问应该怎样做?
我写的是否看得懂?

------解决方案--------------------
if object_id( 't ') is not null
drop table t
go
create table t(C_No varchar(20),C_Area varchar(50),i_Value int)
insert t select '123456 ', 'wh ', 20
union all select '123456 ', 'cs ', 40
union all select '123456 ', 'gz ', 60
union all select '111111 ', 'gz ', 100
union all select '222222 ', 'wh ', 60
union all select '222222 ', 'gz ', 20
union all select '123456 ', 'cs ', 80
go
select * from t
go
select C_Area,sum(i_Value) C_Area,count(C_No) icount from t t1 where i_Value >
(select min(i_Value) from t where c_Area = t1.c_Area) group by C_Area order by c_Area

C_Area C_Area icount
-------------------------------------------------- ----------- -----------
cs 80 1
gz 160 2
wh 60 1

(3 行受影响)