统计取值问题?

insert2003 2007-02-08 11:24:56
有如下表:
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

请问应该怎样做?
我写的是否看得懂?
...全文
298 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cefriend 2007-02-08
  • 打赏
  • 举报
回复
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 行受影响)

cefriend 2007-02-08
  • 打赏
  • 举报
回复
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

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
wangdehao 2007-02-08
  • 打赏
  • 举报
回复
cs 怎么没有?
xikboy 2007-02-08
  • 打赏
  • 举报
回复
看不懂。
rookie_one 2007-02-08
  • 打赏
  • 举报
回复
费解~
xiaoku 2007-02-08
  • 打赏
  • 举报
回复
cs 怎么没有?
insert2003 2007-02-08
  • 打赏
  • 举报
回复
谢谢
cefriend

cs没有是因为
123456 gz 60
只能归入 gz

再次感谢

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧