SELECT a.base_code as base_code, bs.name as base_name, isnull(sum(b.start_number/m.stock_unit_cf),0) as start_number, isnull(sum(b.start_money),0) as start_money, sum(c.cgsh_rk_number/m.stock_unit_cf) as cgsh_rk_number, sum(c.cgsh_rk_money) as cgsh_rk_money, sum(c.cgth_ck_number/m.stock_unit_cf) as cgth_ck_number, sum(c.cgth_ck_money) as cgth_ck_money, sum(c.wldb_rk_number/m.stock_unit_cf) as wldb_rk_number, sum(c.wldb_rk_money) as wldb_rk_money, sum(c.wldb_ck_number/m.stock_unit_cf) as wldb_ck_number, sum(c.wldb_ck_money) as wldb_ck_money, sum(c.wlsy_number/m.stock_unit_cf) as wlsy_number, sum(c.wlsy_money) as wlsy_money, sum(c.xsjk_number/m.stock_unit_cf) as xsjk_number, sum(c.xsjk_money) as xsjk_money, sum((isnull(b.start_number,0)+isnull(c.rk_number,0)-isnull(c.ck_number,0))/m.stock_unit_cf) as end_number, sum(isnull(b.start_money,0)+isnull(c.rk_money,0)-isnull(c.ck_money,0)) as end_money, sum(c.check_number/m.stock_unit_cf) as check_number, sum(c.check_money) as check_money, sum(c.cbtz_money) as cbtz_money, sum(isnull(b.start_money,0)+isnull(c.cgsh_rk_money,0)-isnull(c.cgth_ck_money,0)+isnull(c.wldb_rk_money,0)-isnull(c.wldb_ck_money,0))- sum(isnull(b.start_money,0)+isnull(c.rk_money,0)-isnull(c.ck_money,0)) as cost_money FROM ( SELECT materiel_code, base_code FROM KC_BASE_STOCK a inner join all_materiel m on a.materiel_code=m.code WHERE (1=1) and a.data_area like '00101' and ((m.sort_code in ('00101001','00101002','00101003','00101004','00101005','00101006','00101007','00101008','00101009','00101010','00101011','00101012','00101013','00101014','00101015','00101016','00101017','00101018','00101019','00101020','00101021','00101022','00101023','00101024','00101025','00101026','00101027','00101028','00101029','00101030','00101031','00101032','00101033','00101034','00101036','00101037','00101038','00101039')) and (base_code in ('0010246','0010250'))) GROUP BY materiel_code,base_code ) as a LEFT OUTER JOIN ( SELECT materiel_code, base_code, sum(case when is_rk=1 then number when is_rk=0 then -number else 0.0 end) as start_number, sum(case when is_rk=1 then total_cost when is_rk=0 then -total_cost else 0.0 end) as start_money FROM VKC_CRK_LISTS a inner join all_materiel m on a.materiel_code=m.code WHERE (1=1) and a.data_area like '00101' and ((order_day<20191101) and (m.sort_code in ('00101001','00101002','00101003','00101004','00101005','00101006','00101007','00101008','00101009','00101010','00101011','00101012','00101013','00101014','00101015','00101016','00101017','00101018','00101019','00101020','00101021','00101022','00101023','00101024','00101025','00101026','00101027','00101028','00101029','00101030','00101031','00101032','00101033','00101034','00101036','00101037','00101038','00101039')) and (base_code in ('0010246','0010250'))) GROUP BY materiel_code,base_code ) as b on a.materiel_code=b.materiel_code and a.base_code=b.base_code LEFT OUTER JOIN ( SELECT materiel_code, base_code, sum(case when is_rk=1 then number else 0.0 end) as rk_number, sum(case when is_rk=1 then total_cost else 0.0 end) as rk_money, sum(case when is_rk=0 then number else 0.0 end) as ck_number, sum(case when is_rk=0 then total_cost else 0.0 end) as ck_money, sum(case when is_rk=1 and src_order_type in (21) then number else 0.0 end) as cgsh_rk_number, sum(case when is_rk=1 and src_order_type in (21) then total_cost else 0.0 end) as cgsh_rk_money, sum(case when is_rk=0 and src_order_type in (23) then number else 0.0 end) as cgth_ck_number, sum(case when is_rk=0 and src_order_type in (23) then total_cost else 0.0 end) as cgth_ck_money, sum(case when is_rk=1 and src_order_type in (42) then number else 0.0 end) as wldb_rk_number, sum(case when is_rk=1 and src_order_type in (42) then total_cost else 0.0 end) as wldb_rk_money, sum(case when is_rk=0 and src_order_type in (42) then number else 0.0 end) as wldb_ck_number, sum(case when is_rk=0 and src_order_type in (42) then total_cost else 0.0 end) as wldb_ck_money, sum(case when src_order_type=41 and is_rk=1 then number when src_order_type=41 and is_rk=0 then -number else 0.0 end) as wlsy_number, sum(case when src_order_type=41 and is_rk=1 then total_cost when src_order_type=41 and is_rk=0 then -total_cost else 0.0 end) as wlsy_money, sum(case when src_order_type=45 and is_rk=1 then number when src_order_type=45 and is_rk=0 then -number else 0.0 end) as check_number, sum(case when src_order_type=45 and is_rk=1 then total_cost when src_order_type=45 and is_rk=0 then -total_cost else 0.0 end) as check_money, sum(case when is_rk=0 and src_order_type in (47) then number else 0.0 end) as xsjk_number, sum(case when is_rk=0 and src_order_type in (47) then total_cost else 0.0 end) as xsjk_money, sum(case when src_order_type=60 and is_rk=1 then total_cost when src_order_type=60 and is_rk=0 then -total_cost else 0.0 end) as cbtz_money FROM VKC_CRK_LISTS a inner join all_materiel m on a.materiel_code=m.code WHERE (1=1) and a.data_area like '00101' and ((order_day between 20191101 and 20191130) and (m.sort_code in ('00101001','00101002','00101003','00101004','00101005','00101006','00101007','00101008','00101009','00101010','00101011','00101012','00101013','00101014','00101015','00101016','00101017','00101018','00101019','00101020','00101021','00101022','00101023','00101024','00101025','00101026','00101027','00101028','00101029','00101030','00101031','00101032','00101033','00101034','00101036','00101037','00101038','00101039')) and (base_code in ('0010246','0010250'))) GROUP BY materiel_code,base_code ) as c on a.materiel_code=c.materiel_code and a.base_code=c.base_code INNER JOIN KC_BASE bs on a.base_code=bs.code INNER JOIN ALL_MATERIEL m on a.materiel_code=m.code WHERE (1 = 1) GROUP BY a.base_code,bs.name HAVING abs(isnull(sum(b.start_money),0))>0 or abs(sum(isnull(c.rk_money,0)))>0 or abs(sum(isnull(c.ck_money,0)))>0 or abs(isnull(sum(b.start_number),0))>0 or abs(sum(isnull(c.rk_number,0)))>0 or abs(sum(isnull(c.ck_number,0)))>0 ORDER BY a.base_code