mysql函数

DROP FUNCTION IF EXISTS `getOpPassRate`;
DELIMITER ;;
CREATE  FUNCTION `getOpPassRate`(batchNo varchar(200) , opUrl varchar(200)) RETURNS varchar(200) CHARSET utf8
BEGIN
DECLARE opPassRateStr varchar(200);
DECLARE opPassRate decimal(16,8);
IF opUrl = 'JR'
    THEN set opPassRate = (select count(1) from TT_WO_NAKED_CELL twnc where twnc.BATCH_NO = batchNo and twnc.DEL_FLAG != '1')/((select count(1) from TT_WO_NAKED_CELL twnc where twnc.BATCH_NO = batchNo and twnc.DEL_FLAG != '1') + (select sum(twbs.SCRAP_QTY) from tt_wo_batch_scrap twbs where twbs.BATCH_NO = batchNo and twbs.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and twbs.DEL_FLAG != '1'));
ELSEIF opUrl = 'RK'
    THEN set opPassRate = (select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.RK_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a)/((select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.RK_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a) + (select sum(twbs.SCRAP_QTY) from tt_wo_batch_scrap twbs where twbs.BATCH_NO = batchNo and twbs.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and twbs.DEL_FLAG != '1'));
ELSEIF opUrl = 'DGHJ'
    THEN set opPassRate = (select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.DGHJ_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a)/((select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.DGHJ_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a) + (select sum(twbs.SCRAP_QTY) from tt_wo_batch_scrap twbs where twbs.BATCH_NO = batchNo and twbs.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and twbs.DEL_FLAG != '1'));
ELSEIF opUrl = 'DXHK'
    THEN set opPassRate = (select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.DXHK_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a)/((select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.DXHK_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a) + (select sum(twbs.SCRAP_QTY) from tt_wo_batch_scrap twbs where twbs.BATCH_NO = batchNo and twbs.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and twbs.DEL_FLAG != '1'));
ELSEIF opUrl = 'YCZY'
    THEN set opPassRate = (select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.YCZY_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a)/((select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.YCZY_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a) + (select sum(twbs.SCRAP_QTY) from tt_wo_batch_scrap twbs where twbs.BATCH_NO = batchNo and twbs.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and twbs.DEL_FLAG != '1'));
ELSEIF opUrl = 'HC'
    THEN set opPassRate = (select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.HC_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a)/((select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.HC_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a) + (select sum(twbs.SCRAP_QTY) from tt_wo_batch_scrap twbs where twbs.BATCH_NO = batchNo and twbs.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and twbs.DEL_FLAG != '1'));
ELSEIF opUrl = 'ECZY'
    THEN set opPassRate = (select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.ECZY_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a)/((select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.ECZY_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a) + (select sum(twbs.SCRAP_QTY) from tt_wo_batch_scrap twbs where twbs.BATCH_NO = batchNo and twbs.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and twbs.DEL_FLAG != '1'));
ELSEIF opUrl = 'FR'
    THEN set opPassRate = (select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.FR_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a)/((select count(1) from (select twc.CELL_CODE from tt_wo_cell twc where twc.FR_EQUIP_CODE is not null and twc.DEL_FLAG != '1' group by twc.CELL_CODE) a) + (select sum(twbs.SCRAP_QTY) from tt_wo_batch_scrap twbs where twbs.BATCH_NO = batchNo and twbs.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and twbs.DEL_FLAG != '1'));
ELSE
set opPassRate = (select sum(tww.FINISH_QTY) from tt_wo_wip tww where tww.BATCH_NO = batchNo and tww.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and tww.DEL_FLAG != '1')/((select sum(tww.FINISH_QTY) from tt_wo_wip tww where tww.BATCH_NO = batchNo and tww.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and tww.DEL_FLAG != '1') + (select sum(twbs.SCRAP_QTY) from tt_wo_batch_scrap twbs where twbs.BATCH_NO = batchNo and twbs.OP_ID = (select top.PK_ID from tm_operation top where top.OPERATE_URL = opUrl) and twbs.DEL_FLAG != '1'));
END IF;
IF opPassRate is not null
    then set opPassRateStr = FORMAT(opPassRate*100,2);
END IF;
RETURN opPassRateStr;
END
;;

原文地址:https://www.cnblogs.com/cdcr/p/9438011.html