select t3.khmc
,t3.ckmc
,t4.wlfl
,t4.wllh
,t3.ys
,case when t3.ckmc=t4.ckmc and t3.ys=t4.ys then t3.sl+t4.sl else t3.sl end as sl
from t3,t4
where t3.cplh=t4.wllh/*
khmc ckmc wlfl wllh ys sl
中信 良品 成品 1B089G100-600-GA 银色 1800
中信 可重工品 成品 1B089G100-600-GA 银色 200
中信 不可重工品 成品 1B089G100-600-GA 银色 100
中信 报废品 成品 1B089G100-600-GA 银色 100
中信 良品 成品 1B089G100-600-GA 灰色 100
*/
,t3.ckmc
,t4.wlfl
,t4.wllh
,t3.ys
,case when t3.ckmc=t4.ckmc and t3.ys=t4.ys then t3.sl+t4.sl else t3.sl end as sl
from t3,t4
where t3.cplh=t4.wllh/*
khmc ckmc wlfl wllh ys sl
中信 良品 成品 1B089G100-600-GA 银色 1800
中信 可重工品 成品 1B089G100-600-GA 银色 200
中信 不可重工品 成品 1B089G100-600-GA 银色 100
中信 报废品 成品 1B089G100-600-GA 银色 100
中信 良品 成品 1B089G100-600-GA 灰色 100
*/
这样的话,你两个表格union all一下,然后group by khmc ,ckmc, wlfl,wllh, ys
代码大致如下:
;with data as
(select * from t3
union all
select * from t4
)
select khmc ,ckmc, wlfl,wllh, ys, sum(sl) from data
group by khmc ,ckmc, wlfl,wllh, ys