-- 删除有授权成功的许可记录对应企业的所有失败记录
delete from t_bdm_invoice_permission
where fservergroup = '0'
  and fauthstate != '1'
  and fepinfo in (select t.fepinfo from (select fepinfo from t_bdm_invoice_permission where fservergroup = '0' and fauthstate = '1') t);

-- 只保留开票分组中的一条数据
delete from t_bdm_invoice_permission
where fservergroup = '0'
  and fepinfo in (select t1.fepinfo from (select fepinfo from t_bdm_invoice_permission where fservergroup = '0' group by fepinfo having count(fid) > 1) t1)
  and fid not in (select t2.fid from (select min(fid) fid from t_bdm_invoice_permission where fservergroup = '0' group by fepinfo having count(fid) > 1) t2);

-- 删除没有组织信息并且没有企业信息的数据
delete from t_bdm_invoice_permission where fservergroup = '0' and fepinfo = 0 and forgid = 0;

-- 删除企业表没有授权表有的数据
delete from t_bdm_invoice_permission where fservergroup = '0' and fepinfo not in (select fid from t_bdm_enterprise_baseinfo);