27,580
社区成员
发帖
与我相关
我的任务
分享
create table t1
(
id int,
code varchar(10),
vol1 int,
vol2 int
)
insert into t1
select 1, '01', 11,null union all
select 2, '0101', 12,null union all
select 2, '0102', 13,null union all
select 1, '02', 11,null union all
select 2, '0201', 15,null union all
select 2, '0202', 16,null
;with abc as
(select * from t1 left join
(select id as id1,sum(vol1) as vol11 from t1 where id=1 group by id) a on t1.id=a.id1
left join
(select left(code,2) as code1,sum(vol1) as vol22 from t1 where id=2 group by left(code,2)) b
on left(t1.code,2)=b.code1 and t1.id=2)
select abc.id,abc.code,abc.vol1,case when id=1 then vol11 else vol22 end as vol2
from abc
select *
,f_lhz = (select sum(f_zj1) from table where a.f_jc = f_jc group by f_jc)
from table a
select *
,f_lhz = (select sum(f_zj1) from table group by f_jc)
from table