SELECT
  `d`.`dataId` AS `dataId`,
  `d`.`invoiceJobNumber` AS `invoiceJobNumber`,
  `d`.`agreementType` AS `agreementType`,
  `d`.`approvalNumber` AS `approvalNumber`,
  `d`.`salesContractNumber` AS `salesContractNumber`,
  `d`.`applicationDepartment` AS `applicationDepartment`,
  `d`.`yuJiBaoXiaoDiSanFangChengBenYuan` AS `yuJiBaoXiaoDiSanFangChengBenYuan`,
  `d`.`is_multi_project_merge` AS `is_multi_project_merge`,
  `d`.`is_single_contract_multi_customer` AS `is_single_contract_multi_customer`,
  `d`.`projectNumber` AS `projectNumber`,
  `d`.`customerCompanyName` AS `customerCompanyName`,
  `d`.`customerCompanyNameOther` AS `customerCompanyNameOther`,
  `i`.`piao_hao` AS `piao_hao`,
  `d`.`bu_han_shui_jin_e_yuan_du` AS `bu_han_shui_jin_e_yuan_du`,
  `d`.`han_shui_jin_e_yuan_du` AS `han_shui_jin_e_yuan_du`,
  `d`.`bu_han_shui_jin_e_yuan_da` AS `bu_han_shui_jin_e_yuan_da`,
  `d`.`han_shui_zong_jin_e_yuan_da` AS `han_shui_jin_e_yuan_da`,
  `d`.`bu_han_shui_jin_e_yuan_da_du` AS `bu_han_shui_jin_e_yuan_da_du`,
  `d`.`han_shui_zong_jin_e_yuan_da_du` AS `han_shui_jin_e_yuan_da_du`,
  `d`.`bu_han_shui_jin_e_yuan_duo` AS `bu_han_shui_jin_e_yuan_duo`,
  `d`.`han_shui_jin_e_yuan_duo` AS `han_shui_jin_e_yuan_duo`,
  `d`.`bu_han_shui_jin_e_yuan_other` AS `bu_han_shui_jin_e_yuan_other`,
  `d`.`han_shui_jin_e_yuan_other` AS `han_shui_jin_e_yuan_other`,
  `d`.`applicant` AS `applicant`,
  `d`.`business_content_du` AS `business_content_du`,
  `d`.`uploadedContractAttachment` AS `uploadedContractAttachment`,
  `d`.`uploadedSealedCostListDa` AS `uploadedSealedCostListDa`,
  `d`.`uploadedSealedCostListDu` AS `uploadedSealedCostListDu`,
  `d`.`invoiceType` AS `invoiceType`,
  `d`.`invoiceCode` AS `invoiceCode`,
  `d`.`kai_piao_shi_you` AS `kai_piao_shi_you`,
  `d`.`fa_piao_bei_zhu_lan_tian_xie_nei_rong` AS `fa_piao_bei_zhu_lan_tian_xie_nei_rong`,
  `d`.`tong_yi_she_hui_xin_yong_dai_ma` AS `tong_yi_she_hui_xin_yong_dai_ma`,
  `d`.`bankName` AS `bankName`,
  `d`.`bankAccount` AS `bankAccount`,
  `i`.`invoice_type_enum` AS `invoice_type_enum`,
  `i`.`kai_piao_ri_qi` AS `kai_piao_ri_qi`,
  `i`.`hui_kuan_ri_qi` AS `hui_kuan_ri_qi`,
  `i`.`hui_kuan_jin_e` AS `hui_kuan_jin_e`,
  `i`.`zhang_qi` AS `zhang_qi`,
  `c`.`customer_type` AS `unit_nature`,
  `d`.`hong_chong_piao_hao` AS `hong_chong_piao_hao`
FROM
  (
    (
      (
        (
          SELECT
            `da`.`dataId` AS `dataId`,
            `da`.`invoiceJobNumber` AS `invoiceJobNumber`,
            `da`.`agreementType` AS `agreementType`,
            `da`.`approvalNumber` AS `approvalNumber`,
            `da`.`contractNumber` AS `salesContractNumber`,
            `da`.`applicationDepartment` AS `applicationDepartment`,
            `da`.`yuJiBaoXiaoDiSanFangChengBenYuan` AS `yuJiBaoXiaoDiSanFangChengBenYuan`,
            `da`.`is_multi_project_merge` AS `is_multi_project_merge`,
            `da`.`is_single_contract_multi_customer` AS `is_single_contract_multi_customer`,
            `da`.`projectNumber` AS `projectNumber`,
            `da`.`customerCompanyName` AS `customerCompanyName`,
            `da`.`customerCompanyNameOther` AS `customerCompanyNameOther`,
            `da`.`bu_han_shui_jin_e_yuan_du` AS `bu_han_shui_jin_e_yuan_du`,
            `da`.`han_shui_jin_e_yuan_du` AS `han_shui_jin_e_yuan_du`,
            `da`.`bu_han_shui_jin_e_yuan_da` AS `bu_han_shui_jin_e_yuan_da`,
            `da`.`han_shui_zong_jin_e_yuan_da` AS `han_shui_zong_jin_e_yuan_da`,
            `da`.`bu_han_shui_jin_e_yuan_da_du` AS `bu_han_shui_jin_e_yuan_da_du`,
            `da`.`han_shui_zong_jin_e_yuan_da_du` AS `han_shui_zong_jin_e_yuan_da_du`,
            `da`.`bu_han_shui_jin_e_yuan_duo` AS `bu_han_shui_jin_e_yuan_duo`,
            `da`.`han_shui_jin_e_yuan_duo` AS `han_shui_jin_e_yuan_duo`,
            `da`.`bu_han_shui_jin_e_yuan_other` AS `bu_han_shui_jin_e_yuan_other`,
            `da`.`han_shui_jin_e_yuan_other` AS `han_shui_jin_e_yuan_other`,
            `da`.`applicant` AS `applicant`,
            `da`.`business_content_du` AS `business_content_du`,
            `da`.`uploadedContractAttachment` AS `uploadedContractAttachment`,
            `da`.`uploadedSealedCostListDa` AS `uploadedSealedCostListDa`,
            `da`.`uploadedSealedCostListDu` AS `uploadedSealedCostListDu`,
            `da`.`invoiceType` AS `invoiceType`,
            `da`.`invoiceCode` AS `invoiceCode`,
            `da`.`kai_piao_shi_you` AS `kai_piao_shi_you`,
            `da`.`fa_piao_bei_zhu_lan_tian_xie_nei_rong` AS `fa_piao_bei_zhu_lan_tian_xie_nei_rong`,
            `da`.`tong_yi_she_hui_xin_yong_dai_ma` AS `tong_yi_she_hui_xin_yong_dai_ma`,
            `da`.`bankName` AS `bankName`,
            `da`.`bankAccount` AS `bankAccount`,
            `da`.`hong_chong_piao_hao` AS `hong_chong_piao_hao`,
(
              (
                SELECT
                  count(1)
                FROM
                  `procurement_db`.`dingding_oa_invoice_application` `da2`
                WHERE
                  (
                    (`da2`.`dataId` = `da`.`dataId`)
                    AND (
                      `da2`.`customerCompanyName` = `da`.`customerCompanyName`
                    )
                    AND (
                      (
                        `da2`.`invoiceJobNumber` > `da`.`invoiceJobNumber`
                      )
                      OR (
                        (
                          `da2`.`invoiceJobNumber` = `da`.`invoiceJobNumber`
                        )
                        AND (`da2`.`dataId` > `da`.`dataId`)
                      )
                    )
                  )
              ) + 1
            ) AS `rn_da`,
(
              SELECT
                count(1)
              FROM
                `procurement_db`.`dingding_oa_invoice_application` `da3`
              WHERE
                (
                  (`da3`.`dataId` = `da`.`dataId`)
                  AND (
                    `da3`.`customerCompanyName` = `da`.`customerCompanyName`
                  )
                )
            ) AS `cnt_da`
          FROM
            `procurement_db`.`dingding_oa_invoice_application` `da`
        )
      ) `d`
      LEFT JOIN (
        SELECT
          `ib`.`dataid` AS `dataid`,
          `ib`.`customer_company_name` AS `customer_company_name`,
          `ib`.`piao_hao` AS `piao_hao`,
          `ib`.`invoice_type_enum` AS `invoice_type_enum`,
          `ib`.`kai_piao_ri_qi` AS `kai_piao_ri_qi`,
          `ib`.`hui_kuan_ri_qi` AS `hui_kuan_ri_qi`,
          `ib`.`hui_kuan_jin_e` AS `hui_kuan_jin_e`,
          `ib`.`zhang_qi` AS `zhang_qi`,
(
            (
              SELECT
                count(1)
              FROM
                (
                  SELECT
                    `procurement_db`.`invoice_data`.`dataid` AS `dataid`,
                    `procurement_db`.`invoice_data`.`customer_company_name` AS `customer_company_name`,
                    `procurement_db`.`invoice_data`.`piao_hao` AS `piao_hao`,
                    max(
                      `procurement_db`.`invoice_data`.`invoice_type_enum`
                    ) AS `invoice_type_enum`,
                    max(`procurement_db`.`invoice_data`.`kai_piao_ri_qi`) AS `kai_piao_ri_qi`,
                    max(`procurement_db`.`invoice_data`.`hui_kuan_ri_qi`) AS `hui_kuan_ri_qi`,
                    max(`procurement_db`.`invoice_data`.`hui_kuan_jin_e`) AS `hui_kuan_jin_e`,
                    max(`procurement_db`.`invoice_data`.`zhang_qi`) AS `zhang_qi`
                  FROM
                    `procurement_db`.`invoice_data`
                  GROUP BY
                    `procurement_db`.`invoice_data`.`dataid`,
                    `procurement_db`.`invoice_data`.`customer_company_name`,
                    `procurement_db`.`invoice_data`.`piao_hao`
                ) `ib2`
              WHERE
                (
                  (`ib2`.`dataid` = `ib`.`dataid`)
                  AND (
                    `ib2`.`customer_company_name` = `ib`.`customer_company_name`
                  )
                  AND (
                    (
                      coalesce(`ib2`.`kai_piao_ri_qi`, '9999-12-31') < coalesce(`ib`.`kai_piao_ri_qi`, '9999-12-31')
                    )
                    OR (
                      (
                        coalesce(`ib2`.`kai_piao_ri_qi`, '9999-12-31') = coalesce(`ib`.`kai_piao_ri_qi`, '9999-12-31')
                      )
                      AND (`ib2`.`piao_hao` < `ib`.`piao_hao`)
                    )
                  )
                )
            ) + 1
          ) AS `rn_id`,
(
            SELECT
              count(1)
            FROM
              (
                SELECT
                  `procurement_db`.`invoice_data`.`dataid` AS `dataid`,
                  `procurement_db`.`invoice_data`.`customer_company_name` AS `customer_company_name`,
                  `procurement_db`.`invoice_data`.`piao_hao` AS `piao_hao`
                FROM
                  `procurement_db`.`invoice_data`
                GROUP BY
                  `procurement_db`.`invoice_data`.`dataid`,
                  `procurement_db`.`invoice_data`.`customer_company_name`,
                  `procurement_db`.`invoice_data`.`piao_hao`
              ) `ib3`
            WHERE
              (
                (`ib3`.`dataid` = `ib`.`dataid`)
                AND (
                  `ib3`.`customer_company_name` = `ib`.`customer_company_name`
                )
              )
          ) AS `cnt_id`
        FROM
          (
            SELECT
              `procurement_db`.`invoice_data`.`dataid` AS `dataid`,
              `procurement_db`.`invoice_data`.`customer_company_name` AS `customer_company_name`,
              `procurement_db`.`invoice_data`.`piao_hao` AS `piao_hao`,
              max(
                `procurement_db`.`invoice_data`.`invoice_type_enum`
              ) AS `invoice_type_enum`,
              max(`procurement_db`.`invoice_data`.`kai_piao_ri_qi`) AS `kai_piao_ri_qi`,
              max(`procurement_db`.`invoice_data`.`hui_kuan_ri_qi`) AS `hui_kuan_ri_qi`,
              max(`procurement_db`.`invoice_data`.`hui_kuan_jin_e`) AS `hui_kuan_jin_e`,
              max(`procurement_db`.`invoice_data`.`zhang_qi`) AS `zhang_qi`
            FROM
              `procurement_db`.`invoice_data`
            GROUP BY
              `procurement_db`.`invoice_data`.`dataid`,
              `procurement_db`.`invoice_data`.`customer_company_name`,
              `procurement_db`.`invoice_data`.`piao_hao`
          ) `ib`
      ) `i` ON(
        (
          (`i`.`dataid` = `d`.`dataId`)
          AND (
            `i`.`customer_company_name` = `d`.`customerCompanyName`
          )
          AND (
            `d`.`rn_da` = (((`i`.`rn_id` - 1) % `d`.`cnt_da`) + 1)
          )
        )
      )
    )
    LEFT JOIN (
      SELECT
        `c1`.`customer_unit_name` AS `customer_unit_name`,
        max(`c1`.`customer_type`) AS `customer_type`
      FROM
        (
          `procurement_db`.`customer_management_info` `c1`
          JOIN (
            SELECT
              `procurement_db`.`customer_management_info`.`customer_unit_name` AS `customer_unit_name`,
              max(
                `procurement_db`.`customer_management_info`.`updated_at`
              ) AS `updated_at`
            FROM
              `procurement_db`.`customer_management_info`
            GROUP BY
              `procurement_db`.`customer_management_info`.`customer_unit_name`
          ) `latest` ON(
            (
              (
                `c1`.`customer_unit_name` = `latest`.`customer_unit_name`
              )
              AND (`c1`.`updated_at` = `latest`.`updated_at`)
            )
          )
        )
      GROUP BY
        `c1`.`customer_unit_name`
    ) `c` ON(
      (
        `d`.`customerCompanyName` = `c`.`customer_unit_name`
      )
    )
  )