select 姓名,目的地,出差天数,case when not exists (select * from 出差信息表 where 目的地=a.目的地 and 姓名>a.姓名) then (select sum(出差天数) from 出差信息表 where 目的地=a.目的地) else null end as 总天数 from 出差信息表 a
补充:select 姓名,目的地,出差天数,case when not exists (select * from 出差信息表 where 目的地=a.目的地 and 姓名>a.姓名) then (select sum(出差天数) from 出差信息表 where 目的地=a.目的地) else null end as 总天数 from 出差信息表 a order by 目的地,姓名
多谢!我用你的方法写了,出现如下情况,就是有的统计出来了,却又有的没能统计出来,盼指点迷津!北京用服处 7 NULL 严菲 长春用服处 4 NULL 周勇 长春用服处 7 11 王国平 长沙用服处 7 NULL 黄惠江 成都用服处 7 NULL 姜海军 广州用服处 7 NULL 孔淑蓉 广州用服处 7 NULL 何平select name,object,evec_days,case when not exists (select * from evection where object=a.object and name>a.name) then (select sum(evec_days) from evection where object=a.object) else null end as 总天数 from evection a where a.evec_days<>0 order by object,name
from 出差信息表 a
order by 目的地,姓名
长春用服处 4 NULL 周勇
长春用服处 7 11 王国平
长沙用服处 7 NULL 黄惠江
成都用服处 7 NULL 姜海军
广州用服处 7 NULL 孔淑蓉
广州用服处 7 NULL 何平select name,object,evec_days,case when not exists (select * from evection where object=a.object and name>a.name) then (select sum(evec_days) from evection where object=a.object) else null end as 总天数
from evection a where a.evec_days<>0 order by object,name