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

关于数据汇总(在线等)
数据如下:
cpbh     fzbh         rym1         rym2         rym3       rycl    
  a             1           张三       李四         王五         20
  b             1           张三       李四         王五         30
  c             1           张三       李四         王五         25
  d             1           张三       李四         王五         15
  x             2           AAA         BBB           CCC           32
  y             2           AAA         BBB           CCC           20
  z             2           AAA         BBB           CCC           18
  ...

人员分两组,每组三人,现录入时只输入分组编号即可带出组员名字,三个人产量一样,要求另出一张汇总表如下:

  张三   90
  李四   90
  王五   90
  AAA     70    
  BBB     70
  CCC     70

应该怎么处理呢,请指点!谢谢:)))



------解决方案--------------------
select rym1 as name,sum(rycl) as rycl
from 表
group by rym1
union all
select rym2,sum(rycl)
from 表
group by rym2
union all
select rym3,sum(rycl)
from 表
group by rym3
------解决方案--------------------
create table t(cpbh varchar(10),fzbh int,rym1 varchar(10),rym2 varchar(10),
rym3 varchar(10),rycl int)
insert t select 'a ',1, '张三 ', '李四 ', '王五 ',20
union all select 'b ',1, '张三 ', '李四 ', '王五 ',30
union all select 'c ',1, '张三 ', '李四 ', '王五 ',25
union all select 'd ',1, '张三 ', '李四 ', '王五 ',15
union all select 'x ',2, 'AAA ', 'BBB ', 'CCC ',32
union all select 'y ',2, 'AAA ', 'BBB ', 'CCC ',20
union all select 'z ',2, 'AAA ', 'BBB ', 'CCC ',18

create function fun1(@bh int)
returns @tb table(fzbh int,name varchar(20),cnt int)
as
begin
insert @tb select fzbh,rym1,sum(rycl) as name from t where fzbh=@bh group by rym1,fzbh
union all select fzbh,rym2,sum(rycl) from t where fzbh=@bh group by rym2,fzbh
union all select fzbh,rym3,sum(rycl) from t where fzbh=@bh group by rym3,fzbh
order by fzbh
return
end
go

select name,cnt from fun1(1)