select export_id, dimension, file_name, storage_key, row_count, export_status, export_user_id, export_by, export_time, remark from prc_profit_report_export insert into prc_profit_report_export ( dimension, file_name, storage_key, row_count, export_status, export_user_id, export_by, export_time, remark ) values ( #{dimension}, #{fileName}, #{storageKey}, #{rowCount}, #{exportStatus}, #{exportUserId}, #{exportBy}, sysdate(), #{remark} ) update prc_profit_report_export set file_name = #{fileName}, storage_key = #{storageKey}, row_count = #{rowCount}, export_status = 'SUCCESS', remark = null where export_id = #{exportId} and export_status = 'PROCESSING' update prc_profit_report_export set export_status = 'FAILED', remark = #{remark} where export_id = #{exportId} and export_status = 'PROCESSING' from prc_income_bill i inner join prc_order_bill p on p.bill_id = i.bill_id left join ctr_customer c on c.customer_id = p.customer_id and c.del_flag = '0' and (p.billing_fail_status is null or p.billing_fail_status not in (1, 2, 3)) and c.customer_area = #{customerArea} and p.biz_date >= #{beginBizDate} and p.biz_date <= #{endBizDate} and ( p.order_no like concat('%', #{orderValue}, '%') ) and p.order_no like concat('%', #{orderNo}, '%') and p.customer_code in #{customerValue} and p.customer_code = #{customerCode} and ( p.customer_name like concat('%', #{customerValue}, '%') ) and p.customer_name like concat('%', #{customerName}, '%') and p.product_code <=> #{productCode} and p.country_code <=> #{countryCode} and i.currency <=> #{currency} and i.belong_saler_code = #{belongSalerCode} and i.belong_saler_code = #{belongSalerCode} and p.product_code = #{productCode} and p.country_code = #{countryCode} and i.currency = #{currency} and p.product_name like concat('%', #{productName}, '%') and p.country_name like concat('%', #{countryName}, '%') i.sales_amount is not null and i.redline_amount is not null and not (coalesce(p.billing_fail_status, 0) in (23, 24, 25) and i.sales_amount = 0 and i.redline_amount = 0) null as cost_amount, i.redline_amount, null as redline_gross_profit, null as redline_gross_margin_rate, i.sales_amount, case when then i.sales_amount - i.redline_amount else null end as sales_gross_profit, case when and i.sales_amount != 0 then round((i.sales_amount - i.redline_amount) * 100 / i.sales_amount, 4) else null end as sales_gross_margin_rate i.product_amount as cost_amount, i.redline_amount, case when i.product_amount is not null and i.redline_amount is not null then i.product_amount - i.redline_amount else null end as redline_gross_profit, case when i.product_amount is not null and i.redline_amount is not null and i.redline_amount != 0 then round((i.product_amount - i.redline_amount) * 100 / i.redline_amount, 4) else null end as redline_gross_margin_rate, i.sales_amount, case when i.product_amount is not null and i.sales_amount is not null then i.sales_amount - i.product_amount else null end as sales_gross_profit, case when i.product_amount is not null and i.sales_amount is not null and i.sales_amount != 0 then round((i.sales_amount - i.product_amount) * 100 / i.sales_amount, 4) else null end as sales_gross_margin_rate null as cost_amount, sum(i.redline_amount) as redline_amount, null as redline_gross_profit, null as redline_gross_margin_rate, sum(i.sales_amount) as sales_amount, sum(case when then i.sales_amount - i.redline_amount else null end) as sales_gross_profit, case when sum(case when then i.sales_amount else null end) is null or sum(case when then i.sales_amount else null end) = 0 then null else round(sum(case when then i.sales_amount - i.redline_amount else null end) * 100 / sum(case when then i.sales_amount else null end), 4) end as sales_gross_margin_rate sum(i.product_amount) as cost_amount, sum(i.redline_amount) as redline_amount, sum(case when i.product_amount is not null and i.redline_amount is not null then i.product_amount - i.redline_amount else null end) as redline_gross_profit, case when sum(case when i.product_amount is not null and i.redline_amount is not null then i.redline_amount else null end) is null or sum(case when i.product_amount is not null and i.redline_amount is not null then i.redline_amount else null end) = 0 then null else round( sum(case when i.product_amount is not null and i.redline_amount is not null then i.product_amount - i.redline_amount else null end) * 100 / sum(case when i.product_amount is not null and i.redline_amount is not null then i.redline_amount else null end), 4) end as redline_gross_margin_rate, sum(i.sales_amount) as sales_amount, sum(case when i.product_amount is not null and i.sales_amount is not null then i.sales_amount - i.product_amount else null end) as sales_gross_profit, case when sum(case when i.product_amount is not null and i.sales_amount is not null then i.sales_amount else null end) is null or sum(case when i.product_amount is not null and i.sales_amount is not null then i.sales_amount else null end) = 0 then null else round( sum(case when i.product_amount is not null and i.sales_amount is not null then i.sales_amount - i.product_amount else null end) * 100 / sum(case when i.product_amount is not null and i.sales_amount is not null then i.sales_amount else null end), 4) end as sales_gross_margin_rate