SELECT a.base_code as base_code, base.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.init_rk_number/m.stock_unit_cf) as init_rk_number, sum(c.init_rk_money) as init_rk_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.pssh_rk_number/m.stock_unit_cf) as pssh_rk_number, sum(c.pssh_rk_money) as pssh_rk_money, sum(c.psth_rk_number/m.stock_unit_cf) as psth_rk_number, sum(c.psth_rk_money) as psth_rk_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.yddb_rk_number/m.stock_unit_cf) as yddb_rk_number, sum(c.yddb_rk_money) as yddb_rk_money, sum(c.wlth_rk_number/m.stock_unit_cf) as wlth_rk_number, sum(c.wlth_rk_money) as wlth_rk_money, sum(c.check_rk_number/m.stock_unit_cf) as check_rk_number, sum(c.check_rk_money) as check_rk_money, sum(c.make_rk_number/m.stock_unit_cf) as make_rk_number, sum(c.make_rk_money) as make_rk_money, sum(c.jgfj_rk_number/m.stock_unit_cf) as jgfj_rk_number, sum(c.jgfj_rk_money) as jgfj_rk_money, sum(c.wlsy_rk_number/m.stock_unit_cf) as wlsy_rk_number, sum(c.wlsy_rk_money) as wlsy_rk_money, sum(c.qt_rk_number/m.stock_unit_cf) as qt_rk_number, sum(c.qt_rk_money) as qt_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.pssh_ck_number/m.stock_unit_cf) as pssh_ck_number, sum(c.pssh_ck_money) as pssh_ck_money, sum(c.psth_ck_number/m.stock_unit_cf) as psth_ck_number, sum(c.psth_ck_money) as psth_ck_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.yddb_ck_number/m.stock_unit_cf) as yddb_ck_number, sum(c.yddb_ck_money) as yddb_ck_money, sum(c.make_ck_number/m.stock_unit_cf) as make_ck_number, sum(c.make_ck_money) as make_ck_money, sum(c.wlly_ck_number/m.stock_unit_cf) as wlly_ck_number, sum(c.wlly_ck_money) as wlly_ck_money, sum(c.check_ck_number/m.stock_unit_cf) as check_ck_number, sum(c.check_ck_money) as check_ck_money, sum(c.jgfj_ck_number/m.stock_unit_cf) as jgfj_ck_number, sum(c.jgfj_ck_money) as jgfj_ck_money, sum(c.wlsy_ck_number/m.stock_unit_cf) as wlsy_ck_number, sum(c.wlsy_ck_money) as wlsy_ck_money, sum(c.qt_ck_number/m.stock_unit_cf) as qt_ck_number, sum(c.qt_ck_money) as qt_ck_money, sum(c.xsjk_ck_number/m.stock_unit_cf) as xsjk_ck_number, sum(c.xsjk_ck_money) as xsjk_ck_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 FROM ( SELECT materiel_code, base_code FROM KC_BASE_STOCK WHERE (1=1) and data_area like '00102' GROUP BY materiel_code,base_code ) as a LEFT OUTER JOIN ( select a.materiel_code, a.base_code, sum(start_number) as start_number, sum(start_money) as start_money from ( SELECT materiel_code, base_code, end_number as start_number, end_money as start_money FROM KC_COST_LIST WHERE (cost_id=2424) UNION ALL 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 (order_day >= 20191101 and order_day < 20191101)) and a.data_area like '00102' GROUP BY materiel_code,base_code ) a 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 (31) then number else 0.0 end) as pssh_rk_number, sum(case when is_rk=1 and src_order_type in (31) then total_cost else 0.0 end) as pssh_rk_money, sum(case when is_rk=0 and src_order_type in (31) then number else 0.0 end) as pssh_ck_number, sum(case when is_rk=0 and src_order_type in (31) then total_cost else 0.0 end) as pssh_ck_money, sum(case when is_rk=1 and src_order_type in (32) then number else 0.0 end) as psth_rk_number, sum(case when is_rk=1 and src_order_type in (32) then total_cost else 0.0 end) as psth_rk_money, sum(case when is_rk=0 and src_order_type in (32) then number else 0.0 end) as psth_ck_number, sum(case when is_rk=0 and src_order_type in (32) then total_cost else 0.0 end) as psth_ck_money, sum(case when is_rk=0 and src_order_type in (40) then number else 0.0 end) as wlly_ck_number, sum(case when is_rk=0 and src_order_type in (40) then total_cost else 0.0 end) as wlly_ck_money, sum(case when is_rk=1 and src_order_type in (41) then number else 0.0 end) as wlsy_rk_number, sum(case when is_rk=1 and src_order_type in (41) then total_cost else 0.0 end) as wlsy_rk_money, sum(case when is_rk=0 and src_order_type in (41) then number else 0.0 end) as wlsy_ck_number, sum(case when is_rk=0 and src_order_type in (41) then total_cost else 0.0 end) as wlsy_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 is_rk=1 and src_order_type in (43) then number else 0.0 end) as jgfj_rk_number, sum(case when is_rk=1 and src_order_type in (43) then total_cost else 0.0 end) as jgfj_rk_money, sum(case when is_rk=0 and src_order_type in (43) then number else 0.0 end) as jgfj_ck_number, sum(case when is_rk=0 and src_order_type in (43) then total_cost else 0.0 end) as jgfj_ck_money, sum(case when is_rk=1 and src_order_type in (44) then number else 0.0 end) as wlth_rk_number, sum(case when is_rk=1 and src_order_type in (44) then total_cost else 0.0 end) as wlth_rk_money, sum(case when is_rk=1 and src_order_type in (45) then number else 0.0 end) as check_rk_number, sum(case when is_rk=1 and src_order_type in (45) then total_cost else 0.0 end) as check_rk_money, sum(case when is_rk=0 and src_order_type in (45) then number else 0.0 end) as check_ck_number, sum(case when is_rk=0 and src_order_type in (45) then total_cost else 0.0 end) as check_ck_money, sum(case when is_rk=1 and src_order_type in (46) then number else 0.0 end) as init_rk_number, sum(case when is_rk=1 and src_order_type in (46) then total_cost else 0.0 end) as init_rk_money, sum(case when is_rk=0 and src_order_type in (47) then number else 0.0 end) as xsjk_ck_number, sum(case when is_rk=0 and src_order_type in (47) then total_cost else 0.0 end) as xsjk_ck_money, sum(case when is_rk=1 and src_order_type in (48) then number else 0.0 end) as yddb_rk_number, sum(case when is_rk=1 and src_order_type in (48) then total_cost else 0.0 end) as yddb_rk_money, sum(case when is_rk=0 and src_order_type in (48) then number else 0.0 end) as yddb_ck_number, sum(case when is_rk=0 and src_order_type in (48) then total_cost else 0.0 end) as yddb_ck_money, sum(case when is_rk=1 and src_order_type in (91) then number else 0.0 end) as make_rk_number, sum(case when is_rk=1 and src_order_type in (91) then total_cost else 0.0 end) as make_rk_money, sum(case when is_rk=0 and src_order_type in (91) then number else 0.0 end) as make_ck_number, sum(case when is_rk=0 and src_order_type in (91) then total_cost else 0.0 end) as make_ck_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, sum(case when is_rk=1 and src_order_type >=1000 then number else 0.0 end) as qt_rk_number, sum(case when is_rk=1 and src_order_type >=1000 then total_cost else 0.0 end) as qt_rk_money, sum(case when is_rk=0 and src_order_type >=1000 then number else 0.0 end) as qt_ck_number, sum(case when is_rk=0 and src_order_type >=1000 then total_cost else 0.0 end) as qt_ck_money FROM VKC_CRK_LISTS WHERE (1=1) and data_area like '00102' and (order_day between 20191101 and 20191130) 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 base on a.base_code=base.code INNER JOIN ALL_MATERIEL m on a.materiel_code=m.code WHERE (1 = 1) and ( a.base_code in ('0010476','0010477') 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') ) GROUP BY a.base_code,base.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