create view vi_business_query as SELECT FT.RELATIVESERIALNO                                                                                                            AS RELATIVESERIALNO,
       FT.SERIALNO                                                                                                                    AS SERIALNO,
       FB.CONTRACT_ID                                                                                                                 AS CONTRACT_ID,
       FB.PROJ_ID                                                                                                                     AS PROJ_ID,
       FB.FLOW_UNID                                                                                                                   AS FLOW_UNID,
       O.PHASENO                                                                                                                      AS PHASENO,
       O.FLOWNAME                                                                                                                     AS FLOWNAME,
       O.APPLYTYPE                                                                                                                    AS APPLYTYPE,
       O.PHASETYPE                                                                                                                    AS PHASETYPE,
       O.OBJECTNO                                                                                                                     AS OBJECTNO,
       O.OBJECTTYPE                                                                                                                   AS OBJECTTYPE,
       FT.USERID                                                                                                                      AS USERID,
       O.FLOWNO                                                                                                                       AS FLOWNO,
       FB.FLOW_KEY                                                                                                                    AS FLOW_KEY,
       LUL.CUSTOMER_NAME                                                                                                              AS CUSTOMER_NAME,
       DI.DISTRIBUTOR_NAME                                                                                                            AS DISTRIBUTOR_NAME,
       (CASE O.PHASENAME WHEN '结束' THEN '审批通过' WHEN '通过' THEN '审批通过' WHEN '否决' THEN '审批不通过' WHEN '经销商发起' THEN '退回经销商' ELSE '审批中' END) AS PHASENAME,
       FT.USERNAME                                                                                                                    AS USERNAME,
       max(FT.BEGINTIME)                                                                                                              AS BEGINTIME,
       FT.ENDTIME                                                                                                                     AS ENDTIME
FROM (((((APZL.FLOW_OBJECT O LEFT JOIN APZL.FLOW_BUSSINESS_OBJECT FB ON ((O.OBJECTNO = FB.FLOW_UNID))) LEFT JOIN APZL.FLOW_TASK FT ON (((FT.OBJECTNO = O.OBJECTNO) AND (FT.PHASENO = O.PHASENO)))) LEFT JOIN APZL.LB_PROJECT_INFO_TEMP LPI ON ((LPI.ID = convert(FB.PROJ_ID USING UTF8)))) LEFT JOIN APZL.DISTRIBUTOR_INFO DI ON ((DI.DISTRIBUTOR_NO = LPI.DISTRIBUTOR_ID)))
LEFT JOIN APZL.LB_UNION_LESSEE_TEMP LUL ON ((convert(LUL.PROJECT_ID USING UTF8) = LPI.ID)))
WHERE ((LPI.BUSINESSTYPE = '1') AND (O.FLOWNO = 'BusinessApplyFlow') AND (FT.RELATIVESERIALNO IS NOT NULL))
GROUP BY FB.FLOW_UNID
UNION ALL
SELECT FT.RELATIVESERIALNO                                       AS RELATIVESERIALNO,
       FT.SERIALNO                                               AS SERIALNO,
       FB.CONTRACT_ID                                            AS CONTRACT_ID,
       FB.PROJ_ID                                                AS PROJ_ID,
       FB.FLOW_UNID                                              AS FLOW_UNID,
       O.PHASENO                                                 AS PHASENO,
       O.FLOWNAME                                                AS FLOWNAME,
       O.APPLYTYPE                                               AS APPLYTYPE,
       O.PHASETYPE                                               AS PHASETYPE,
       O.OBJECTNO                                                AS OBJECTNO,
       O.OBJECTTYPE                                              AS OBJECTTYPE,
       FT.USERID                                                 AS USERID,
       O.FLOWNO                                                  AS FLOWNO,
       FB.FLOW_KEY                                               AS FLOW_KEY,
       LUL.CUSTOMER_NAME                                         AS CUSTOMER_NAME,
       DI.DISTRIBUTOR_NAME                                       AS DISTRIBUTOR_NAME,
       (CASE O.PHASENAME WHEN '结束' THEN '审批通过后撤销' ELSE NULL END) AS PHASENAME,
       FT.USERNAME                                               AS USERNAME,
       max(FT.BEGINTIME)                                         AS BEGINTIME,
       FT.ENDTIME                                                AS ENDTIME
FROM (((((APZL.FLOW_OBJECT O LEFT JOIN APZL.FLOW_BUSSINESS_OBJECT FB ON ((O.OBJECTNO = FB.FLOW_UNID))) LEFT JOIN APZL.FLOW_TASK FT ON (((FT.OBJECTNO = O.OBJECTNO) AND (FT.PHASENO = O.PHASENO)))) LEFT JOIN APZL.LB_PROJECT_INFO_TEMP LPI ON ((LPI.ID = convert(FB.PROJ_ID USING UTF8)))) LEFT JOIN APZL.DISTRIBUTOR_INFO DI ON ((DI.DISTRIBUTOR_NO = LPI.DISTRIBUTOR_ID)))
LEFT JOIN APZL.LB_UNION_LESSEE_TEMP LUL ON ((convert(LUL.PROJECT_ID USING UTF8) = LPI.ID)))
WHERE ((LPI.BUSINESSTYPE = '1') AND (O.FLOWNO = 'BusinessCancelFlow') AND (FT.RELATIVESERIALNO IS NOT NULL) AND (O.PHASENAME = '结束'))
GROUP BY FB.FLOW_UNID;

