T2呢?
--------
SELECT T.*,T.母工单号||T.工单状态 FROM (
select t0.SFAADOCNO as 工单号, t0.SFAA006 as 来源单号, t0.SFAA010 as 生产料号, t0.SFAA012 as 订单数, t0.SFAA049 as 已发料套数, t0.SFAA019 as 预计开工日期, t0.SFAA020 as 预计完工日期, t0.SFAA049 as 已发料套数, t0.SFAA050 as 已入库合格量, substr(t1.IMAAL004,1,13) as 工单图号,
case when t0.SFAA003 = '1' then '一般工单' when t0.SFAA003 = '2' then '返修工单' else '其他' end as 工单类型,
case when t0.SFAA057 = '1' then '厂内' when t0.SFAA057 = '2' then '委外' else '其他' end as 委外类型,
case when t0.SFAASTUS = 'A' then '已核准' when t0.SFAASTUS = 'C' then '结案' when t0.SFAASTUS = 'D' then '抽单' when t0.SFAASTUS = 'F' then '已发出' when t0.SFAASTUS = 'M' then '成本结案' when t0.SFAASTUS = 'N' then '未审核' when t0.SFAASTUS = 'R' then '已拒绝' when t0.SFAASTUS = 'W' then '送签中' when t0.SFAASTUS = 'X' then '作废' when t0.SFAASTUS = 'Y' then '已审核' else '其他' end as 工单状态,
case when t0.SFAA021 is NULL then t0.SFAADOCNO ELSE t0.SFAA021 END as 母工单号,
case when substr(t2.SFAA010,1,13) is NULL then t0.SFAA010 ElSE substr(t2.SFAA010,1,13) END as 母工单料号2,
t3.SFDB007 as 工单发料套数, t4.SFFB017 as 报工数量 from DSDATA.SFAA_T t0
left join DSDATA.IMAAL_T t1 on t1.IMAAL001 = t0.SFAA010
left join DSDATA.SFAA_T t2 on t2.SFAADOCNO = t0.SFAA021
left join DSDATA.SFDB_T t3 on t3.SFDB001 = t0.SFAADOCNO
left join DSDATA.SFFB_T t4 on t4.SFFB005 = t0.SFAADOCNO and t4.SFFB017 not in 0
where t0.SFAASITE in 'ZZDJ'
) T