/*dialect*/
with tab1 as(
select distinct
entry.FManuBillID manuOrderId,
sum(entry.fqty) qty--良品入库数量
from t_im_manufacturerecbill bill
inner join t_im_manufacturerecbillentry entry on entry.fparentid = bill.fid
inner join t_scm_transactiontype transType on transType.Fid = bill.ftransactiontypeid
inner join T_ORG_Storage tos on tos.fid = bill.FStorageOrgUnitID
inner join t_mm_manufactureorder tmm on tmm.FMaterialID = entry.FMaterialID
inner join T_BD_Material tbm on tbm.fid = entry.FMaterialID
inner join t_db_warehouse warehouse on warehouse.fid = entry.fwarehouseid
where entry.FManuBillID = tmm.fid
and bill.fbasestatus = 4
and warehouse.fname_l2 not like '%次品%'
and warehouse.fname_l2 not like '%废%'
and warehouse.fname_l2 not like '%B品%'
and (tos.fid in ('gJYAAAAABN/M567U') or 0 is null)
and (tbm.fnumber >= '' or null is null)
and (tbm.fnumber <= '' or null is null)
and(tmm.fnumber in '' or null is null)
and(tmm.FAdminOrgUnitID in '' or null is null)
and to_char(tmm.FBizDate,'yyyy-MM-dd') <= '2018-06-08'
and to_char(tmm.FBizDate,'yyyy-MM-dd') >= '2017-10-01'
and (tmm.fbasestatus = '' or null is null)
and transType.Fnumber in ('024A','026C','902A','902D','901','901C')--生产良品入库、生产入库(返工)、股份公司-委外加工-良品入库、股份公司-委外加工-样品入库、受托加工良品、样品
group by entry.FManuBillID
)
,
tab2 as(
--B品
select distinct
entry.FManuBillID manuOrderId,
sum(entry.fqty) qty
from t_im_manufacturerecbill bill
inner join t_im_manufacturerecbillentry entry on entry.fparentid = bill.fid
inner join t_scm_transactiontype transType on transType.Fid = bill.ftransactiontypeid
inner join T_ORG_Storage tos on tos.fid = bill.FStorageOrgUnitID
inner join t_mm_manufactureorder tmm on tmm.fid = entry.fmanubillid
inner join t_db_warehouse warehouse on warehouse.fid = entry.fwarehouseid
inner join T_BD_Material tbm on tbm.fid = entry.FMaterialID
where (tos.fid in ('gJYAAAAABN/M567U') or 0 is null)
and to_char(tmm.FBizDate,'yyyy-MM-dd') <= '2018-06-08'
and to_char(tmm.FBizDate,'yyyy-MM-dd') >= '2017-10-01'
and (tbm.fnumber >= '' or null is null)
and (tbm.fnumber <= '' or null is null)
and(tmm.fnumber in '' or null is null)
and(tmm.FAdminOrgUnitID in '' or null is null)
and warehouse.fname_l2 like '%B品%'
-- and tmm.fnumber = 'MLS-1-20180306-0001'
and (tmm.fbasestatus = '' or null is null)
and bill.fbasestatus = 4
and transType.Fnumber not in ('024A','902A','902D','901','901C')--生产良品入库、股份公司-委外加工-良品入库、股份公司-委外加工-样品入库、受托加工良品、样品
group by entry.FManuBillID
)
,
tab3 as(
---次品
select distinct
entry.FManuBillID manuOrderId
,sum(entry.fqty) qty
from t_im_manufacturerecbill bill
inner join t_im_manufacturerecbillentry entry on entry.fparentid = bill.fid
inner join T_ORG_Storage tos on tos.fid = bill.FStorageOrgUnitID
inner join t_scm_transactiontype transType on transType.Fid = bill.ftransactiontypeid
inner join t_db_warehouse warehouse on warehouse.fid = entry.fwarehouseid
inner join T_BD_Material tbm on tbm.fid = entry.FMaterialID
inner join t_mm_manufactureorder tmm on tmm.fid = entry.fmanubillid
where (tos.fid in ('gJYAAAAABN/M567U')or 0 is null)
and to_char(tmm.FBizDate,'yyyy-MM-dd') <= '2018-06-08'
and to_char(tmm.FBizDate,'yyyy-MM-dd') >= '2017-10-01'
and transType.Fnumber not in ('024A','902A','902D','901','901C')--生产良品入库、股份公司-委外加工-良品入库、股份公司-委外加工-样品入库、受托加工良品、样品
and (tbm.fnumber >= '' or null is null)
and (tbm.fnumber <= '' or null is null)
and(tmm.fnumber in '' or null is null)
and(tmm.FAdminOrgUnitID in '' or null is null)
and warehouse.fname_l2 like '%次品%'
and (tmm.fbasestatus = '' or null is null)
and bill.fbasestatus = 4
group by entry.FManuBillID
),
tab4 as(
--废品
select distinct
entry.FManuBillID manuOrderId
,sum(entry.fqty) qty
from t_im_manufacturerecbill bill
inner join t_im_manufacturerecbillentry entry on entry.fparentid = bill.fid
inner join T_ORG_Storage tos on tos.fid = bill.FStorageOrgUnitID
inner join t_scm_transactiontype transType on transType.Fid = bill.ftransactiontypeid
inner join t_db_warehouse warehouse on warehouse.fid = entry.fwarehouseid
inner join T_BD_Material tbm on tbm.fid = entry.FMaterialID
inner join t_mm_manufactureorder tmm on tmm.fid = entry.fmanubillid
where tos.fid in ('gJYAAAAABN/M567U')
and to_char(tmm.FBizDate,'yyyy-MM-dd') <= '2018-06-08'
and to_char(tmm.FBizDate,'yyyy-MM-dd') >= '2017-10-01'
and transType.Fnumber not in ('024A','902A','902D','901','901C')--生产良品入库、股份公司-委外加工-良品入库、股份公司-委外加工-样品入库、受托加工良品、样品
and (tbm.fnumber >= '' or null is null)
and (tbm.fnumber <= '' or null is null)
and(tmm.fnumber = '' or null is null)
and(tmm.FAdminOrgUnitID = '' or null is null)
and warehouse.fname_l2 like '%废%'
and (tmm.fbasestatus = '' or null is null)
and bill.fbasestatus = 4
group by entry.FManuBillID
)
,tab5 as(
select distinct
tmm.fnumber scddh
,sum(time1.fqty) sl
from
T_IM_MaterialReqBill tim
INNER join T_IM_MaterialReqBillEntry time1 on time1.fparentid = tim.fid
INNER join T_MM_ManufactureOrder tmm on tmm.fnumber = time1.FOrderNumber
INNER join T_BD_Material tbm on tbm.fid = time1.FMaterialID
where to_char(tmm.FBizDate,'yyyy-MM-dd') <= '2018-06-08'
and to_char(tmm.FBizDate,'yyyy-MM-dd') >= '2017-10-01'
AND (tbm.fnumber like '01.01.02%' or tbm.fnumber like '02.03.20%' or tbm.fnumber like '05.10.03.003%')
and tim.fbasestatus = 4
group by tmm.fnumber
ORDER BY tmm.fnumber DESC
)
,tab6 as (

select distinct
tmm.fnumber scddh
,sum(time1.fqty) sl
from
T_IM_MaterialReqBill tim
INNER join T_IM_MaterialReqBillEntry time1 on time1.fparentid = tim.fid
INNER join T_MM_ManufactureOrder tmm on tmm.fnumber = time1.FOrderNumber
left join T_BD_Material tbm on tbm.fid = time1.FMaterialID
where to_char(tmm.FBizDate,'yyyy-MM-dd') <= '2018-06-08'
and to_char(tmm.FBizDate,'yyyy-MM-dd') >= '2017-10-01'
AND (tbm.fnumber like '01.01.01.010%' or tbm.fnumber like '02.10.03.001%' or tbm.fnumber like '05.10.22.001%')
and tim.fbasestatus = 4
group by tmm.fnumber

ORDER BY tmm.fnumber DESC
)
select
tmm.fnumber 生产订单号, --生产订单号
to_char(tmm.fbizdate,'yyyy-MM-dd') 业务日期,
toa.Fname_l2 成本中心,
tbm.fnumber 物料编码, --物料编码
tbm.fname_l2 物料名称, --物料名称
tbm.fmodel 规格型号, --规格型号
nvl(tbh.fgoodrates,0) 标准良率,--标准良率
to_char(tbh.feffectdate,'yyyy-MM-dd') 生效日期, --生效日期
to_char(tbh.fexpirydate,'yyyy-MM-dd') 失效日期, --失效日期
tbm1.fname_l2 单位, --单位
tmm.FQty 生产订单数量, --生产订单数量
nvl(tab1.qty,0) 良品入库数量, --良品入库数量
nvl(bp.qty,0) B品入库数量, --次品入库数量
nvl(cp.qty,0) 次品入库数量, --废品入库数量
nvl(fp.qty,0) 废品入库数量, --B品入库数量
tmm.FQty- nvl(tab1.qty,0)-nvl(bp.qty,0)-nvl(cp.qty,0)-nvl(fp.qty,0) 未入库数量,
decode(tmm.FQty- nvl(tab1.qty,0)-nvl(bp.qty,0)-nvl(cp.qty,0)-nvl(fp.qty,0),'0','OK') 是否清单,--增了这里
cast( decode(tab1.qty*100,0,0,nvl(tab1.qty,0)/tmm.FQty*100) as number(18,2))良品实际入库比例,--增了这里
(cast( decode(tab1.qty*100,0,0,nvl(tab1.qty,0)/tmm.FQty*100) as number(18,2))) + (cast( decode(tmm.FQty*100,0,0,nvl(bp.qty,0)/tmm.FQty*100) as number(18,2))) 良品B品实际入库比例,--增了这里
cast( decode(tmm.FQty*100,0,0,nvl(bp.qty,0)/tmm.FQty*100) as number(18,2))B品实际入库比例,
cast(decode(tmm.FQty*100,0,0,nvl(cp.qty,0)/tmm.FQty*100) as number(18,2))次品实际入库比例,--次品实际入库比例%(次品入库数量/生产订单数量*100)
cast(decode(tmm.FQty*100,0,0, nvl(fp.qty,0)/tmm.FQty*100)as number(18,2))废品实际入库比例, --废品实际入库比例%(废品入库数量/生产订单数量*100)
cast(decode(tmm.FQty*100,0,0,100-tbh.fgoodrates-(nvl(bp.qty,0)/tmm.FQty*100+ nvl(cp.qty,0)/tmm.FQty*100+nvl(fp.qty,0)/tmm.FQty*100)) as number(18,2)) B次废品不良率差异,--增了这里
to_char(tmm.FCreateTime,'yyyy-MM-dd') 订单日期,--增了这里
to_char(tmm.FCreateTime+30,'yyyy-MM-dd') 到期日期,--增了这里
case when to_char(add_months(tmm.FCreateTime,1),'yyyy-MM-dd') <= to_char(sysdate,'yyyy-MM-dd') then '到期' else '' end 到期提示
,to_char(sysdate,'yyyy-MM-dd') 当天日期, --增了这里
cbc.cfparticlecount 芯粒数3--增了这里
,t5.sl 领支架
,t6.sl 领芯片
,case when t6.sl/cbc.cfparticlecount-t5.sl=0 then '是' else '否' end 匹配提示
,decode(tmm.fbasestatus,'1','保存','2','提交','3','作废','4','审核','5','下达','6','冻结','7','关闭','8','完工','其他') 单据状态
,tmm.fremark_l2 备注
from T_MM_ManufactureOrder tmm
left join T_BAS_YieldBaseData tbh on tbh.fproductnumberid = tmm.fmaterialid and tmm.FStorageOrgUnitID = tbh.forgunitid and tbh.fstatus = '1'
left join T_BD_MeasureUnit tbm1 on tbm1.fid = tmm.FUnitID
left join T_ORG_Storage tos on tos.fid = tmm.FStorageOrgUnitID
left join tab1 on tab1.manuOrderId = tmm.fid
left join tab2 bp on bp.manuOrderId = tmm.fid
left join tab3 cp on cp.manuOrderId = tmm.fid
left join tab4 fp on fp.manuOrderId = tmm.fid
left join tab5 t5 on t5.scddh = tmm.fnumber
left join tab6 t6 on t6.scddh = tmm.fnumber --生产订单单号关联
left join T_ORG_Admin toa on toa.fid = tmm.FAdminOrgUnitID
left join T_BD_Material tbm on tbm.fid = tmm.FMaterialID
left join T_IM_MaterialReqBillEntry entry1 on tmm.fnumber= entry1.fordernumber --增了这里
inner join T_IM_MaterialReqBill tim on tim.fid = entry1.fparentid
left join ct_bas_coreprtnumber cbc on cbc.cfmaterailnumberid = tbm.fid --增了这里
where 1=1
and (tos.fid in ('gJYAAAAABN/M567U') or 0 is null)
and to_char(tmm.FBizDate,'yyyy-MM-dd') <= '2018-06-08'
and to_char(tmm.FBizDate,'yyyy-MM-dd') >= '2017-10-01'
and (tbm.fnumber >= '' or null is null)
and (tbm.fnumber <= '' or null is null)
and(tmm.fnumber in '' or null is null)
and(tmm.FAdminOrgUnitID in ('') or null is null)
and (tmm.fbasestatus = '' or null is null)
and tmm.fbasestatus = '5'
and tim.fbasestatus = '4'
group by tmm.fnumber
,to_char(tmm.fbizdate,'yyyy-MM-dd')
,toa.Fname_l2,substr(tbm.fnumber,0,8)
,substr(tbm.fnumber,0,12)
,tbm.fname_l2,tbm.fmodel
,nvl(tbh.fgoodrates,0)
,to_char(tbh.feffectdate,'yyyy-MM-dd')
,to_char(tbh.fexpirydate,'yyyy-MM-dd')
,tbm1.fname_l2,tmm.FQty
,nvl(tab1.qty,0)
,nvl(bp.qty,0)
,nvl(cp.qty,0)
,nvl(fp.qty,0)
,cast( decode(tab1.qty*100,0,0,nvl(tab1.qty,0)/tmm.FQty*100) as number(18,2))
,cast(decode(tmm.FQty*100,0,0,100-tbh.fgoodrates-(nvl(bp.qty,0)/tmm.FQty*100+ nvl(cp.qty,0)/tmm.FQty*100+nvl(fp.qty,0)/tmm.FQty*100)) as number(18,2))
,to_char(tmm.FCreateTime,'yyyy-MM-dd') ,to_char(add_months(tmm.FCreateTime,1),'yyyy-MM-dd')
,cbc.cfparticlecount
,t5.sl
,t6.sl
,case when to_char(add_months(tmm.FCreateTime,1),'yyyy-MM-dd') <= to_char(sysdate,'yyyy-MM-dd') then '到期' else '' end
,decode(tmm.fbasestatus,'1','保存','2','提交','3','作废','4','审核','5','下达','6','冻结','7','关闭','8','完工','其他')
,tmm.fremark_l2,tbm.fnumber, to_char(tmm.FCreateTime+30,'yyyy-MM-dd')
order by toa.Fname_l2 asc
(在领料出库的时候支架是跨库存领的,所以在界面中看不到,要选择多个库存组织)

(如果进行截取字符串分组就写子查询!注意一定要对应上查询条件)