WITH counts AS (
  SELECT
    SUM(Fluid_Inbound_Merge_1 = 1) AS Fluid_Inbound_Merge_1,
    SUM(Fluid_Inbound_Merge_2 = 1) AS Fluid_Inbound_Merge_2,
    SUM(Fluid_Inbound_Merge_3 = 1) AS Fluid_Inbound_Merge_3,
    SUM(Fluid_Inbound_Merge_4 = 1) AS Fluid_Inbound_Merge_4,
    SUM(Fluid_Inbound_Merge_5 = 1) AS Fluid_Inbound_Merge_5,
    SUM(Fluid_Inbound_Merge_6 = 1) AS Fluid_Inbound_Merge_6,
    SUM(Fluid_Inbound_Merge_7 = 1) AS Fluid_Inbound_Merge_7,
    SUM(Non_Con_Merge = 1) AS Non_Con_Merge,
    SUM(NCP = 1) AS NCP,
    SUM(Bulk_Inbound_MCM04 = 1) AS Bulk_Inbound_MCM04,
    SUM(Fluid_Outbound_MCM04 = 1) AS Fluid_Outbound_MCM04,
    SUM(Problem_Solve_MCM04 = 1) AS Problem_Solve_MCM04,
    SUM(Bulk_Inbound_MCM05 = 1) AS Bulk_Inbound_MCM05,
    SUM(Fluid_Outbound_MCM05 = 1) AS Fluid_Outbound_MCM05,
    SUM(Problem_Solve_MCM05 = 1) AS Problem_Solve_MCM05,
    SUM(NCS1 = 1) AS NCS1,
    SUM(NCS2 = 1) AS NCS2,
    SUM(BYB = 1) AS BYB,
    SUM(BYA = 1) AS BYA,
    SUM(BYC = 1) AS BYC,
    SUM(BYD = 1) AS BYD,
    SUM(Chutes_MCM04 = 1) AS Chutes_MCM04,
    SUM(Chutes_MCM05 = 1) AS Chutes_MCM05
  FROM jam_area
  WHERE t_stamp BETWEEN :starttime AND :endtime
),
totals AS (
  SELECT 
    COALESCE(Fluid_Inbound_Merge_1,0) + COALESCE(Fluid_Inbound_Merge_2,0) + 
    COALESCE(Fluid_Inbound_Merge_3,0) + COALESCE(Fluid_Inbound_Merge_4,0) + 
    COALESCE(Fluid_Inbound_Merge_5,0) + COALESCE(Fluid_Inbound_Merge_6,0) + 
    COALESCE(Fluid_Inbound_Merge_7,0) + COALESCE(Non_Con_Merge,0) + 
    COALESCE(NCP,0) + COALESCE(Bulk_Inbound_MCM04,0) + 
    COALESCE(Fluid_Outbound_MCM04,0) + COALESCE(Problem_Solve_MCM04,0) + 
    COALESCE(Bulk_Inbound_MCM05,0) + COALESCE(Fluid_Outbound_MCM05,0) + 
    COALESCE(Problem_Solve_MCM05,0) + COALESCE(NCS1,0) + COALESCE(NCS2,0) + 
    COALESCE(BYB,0) + COALESCE(BYA,0) + COALESCE(BYC,0) + COALESCE(BYD,0) + 
    COALESCE(Chutes_MCM04,0) + COALESCE(Chutes_MCM05,0) AS Total
  FROM counts
)
SELECT
  DATE_FORMAT(:starttime, '%Y-%m-%d %H:%i') AS StartTimestamp,
  DATE_FORMAT(:endtime,   '%Y-%m-%d %H:%i') AS EndTimestamp,
  t.Total,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Fluid_Inbound_Merge_1,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Fluid_Inbound_Merge_1,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Fluid_Inbound_Merge_2,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Fluid_Inbound_Merge_2,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Fluid_Inbound_Merge_3,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Fluid_Inbound_Merge_3,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Fluid_Inbound_Merge_4,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Fluid_Inbound_Merge_4,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Fluid_Inbound_Merge_5,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Fluid_Inbound_Merge_5,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Fluid_Inbound_Merge_6,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Fluid_Inbound_Merge_6,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Fluid_Inbound_Merge_7,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Fluid_Inbound_Merge_7,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Non_Con_Merge,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Non_Con_Merge,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.NCP,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS NCP,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Bulk_Inbound_MCM04,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Bulk_Inbound_MCM04,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Fluid_Outbound_MCM04,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Fluid_Outbound_MCM04,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Problem_Solve_MCM04,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Problem_Solve_MCM04,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Bulk_Inbound_MCM05,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Bulk_Inbound_MCM05,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Fluid_Outbound_MCM05,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Fluid_Outbound_MCM05,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Problem_Solve_MCM05,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Problem_Solve_MCM05,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.NCS1,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS NCS1,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.NCS2,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS NCS2,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.BYB,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS BYB,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.BYA,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS BYA,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.BYC,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS BYC,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.BYD,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS BYD,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Chutes_MCM04,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Chutes_MCM04,
  CASE WHEN t.Total = 0 THEN '0%' ELSE CONCAT(ROUND(COALESCE(c.Chutes_MCM05,0) / NULLIF(t.Total,0) * 100, 2), '%') END AS Chutes_MCM05
FROM counts c
CROSS JOIN totals t;
