 SELECT bd.project_id,
    bd.master_product_id,
    bd.process_stage_id,
    ((((replace(to_char(round((min(bd.batch_cycle_time) / (60)::double precision)), 'FM99999'::text), ' '::text, '0'::text) || ' hrs'::text) || ' : '::text) || replace(to_char(round((((min(bd.batch_cycle_time))::integer % 60))::double precision), 'FM99'::text), ' '::text, '0'::text)) || ' mins'::text) AS min_bct,
    ((((replace(to_char(round((avg(bd.batch_cycle_time) / (60)::double precision)), 'FM99999'::text), ' '::text, '0'::text) || ' hrs'::text) || ' : '::text) || replace(to_char(round((((avg(bd.batch_cycle_time))::integer % 60))::double precision), 'FM99'::text), ' '::text, '0'::text)) || ' mins'::text) AS avg_bct,
    ((((replace(to_char(round((max(b.golde_batch_time) / (60)::double precision)), 'FM99999'::text), ' '::text, '0'::text) || ' hrs'::text) || ' : '::text) || replace(to_char(round((((max(b.golde_batch_time))::integer % 60))::double precision), 'FM99'::text), ' '::text, '0'::text)) || ' mins'::text) AS golden_bct,
    concat((max(t.ideal_batch_cycle_time))::text, ' hrs : ', '00 mins') AS ideal_bct,
    round((max(f.avg_quality))::numeric, 2) AS avg_quality,
    round((max(f.ideal_quality))::numeric, 2) AS ideal_quality,
    round((max(j.kpi_value))::numeric, 2) AS golden_batch_quality
   FROM ((((batch_details bd
     LEFT JOIN ( SELECT bd_1.batch_no,
            bd_1.process_stage_id,
            bd_1.master_product_id,
            bd_1.batch_cycle_time AS golde_batch_time,
            bd_1.project_id
           FROM batch_details bd_1
          WHERE (bd_1.golden_batch IS TRUE)) b ON ((((b.project_id)::text = (bd.project_id)::text) AND ((b.process_stage_id)::text = (bd.process_stage_id)::text) AND ((b.master_product_id)::text = (bd.master_product_id)::text))))
     LEFT JOIN ( SELECT DISTINCT bd_1.process_stage_id,
            bd_1.master_product_id,
            bd_1.ideal_batch_cycle_time,
            bd_1.project_id
           FROM batch_details bd_1
          WHERE (bd_1.ideal_batch_cycle_time IS NOT NULL)) t ON ((((t.project_id)::text = (bd.project_id)::text) AND ((t.process_stage_id)::text = (bd.process_stage_id)::text) AND ((t.master_product_id)::text = (bd.master_product_id)::text))))
     LEFT JOIN ( SELECT bq2.process_stage_id,
            bq2.kpi_name,
            avg(bq2.kpi_value) AS avg_quality,
            avg(bq2.kpi_value) AS ideal_quality,
            bq2.project_id
           FROM batch_quality bq2
          WHERE (bq2.critical_parameter IS TRUE)
          GROUP BY bq2.process_stage_id, bq2.kpi_name, bq2.project_id) f ON ((((f.project_id)::text = (bd.project_id)::text) AND ((f.process_stage_id)::text = (bd.process_stage_id)::text))))
     LEFT JOIN ( SELECT bq.batch_no,
            bq.process_stage_id,
            bq.kpi_name,
            bq.kpi_value,
            bq.project_id
           FROM batch_quality bq
          WHERE (bq.critical_parameter IS TRUE)) j ON ((((j.project_id)::text = (b.project_id)::text) AND ((j.batch_no)::text = (b.batch_no)::text) AND ((j.process_stage_id)::text = (b.process_stage_id)::text))))
  WHERE ((bd.batch_start > (now() - '6 mons'::interval)) AND ((bd.batch_status)::text = 'completed'::text))
  GROUP BY bd.project_id, bd.master_product_id, bd.process_stage_id;