SELECT
  DATE_FORMAT(:starttime, '%Y-%m-%d %H:%i') AS StartTimestamp,
  DATE_FORMAT(:endtime,   '%Y-%m-%d %H:%i') AS EndTimestamp,

  -- totals
  COALESCE(c.U1,0) + COALESCE(c.U2,0) + COALESCE(c.U3,0) + COALESCE(c.U4,0) AS Total,

  -- percentages (string with %; 0% when total = 0)
  CASE 
    WHEN (COALESCE(c.U1,0) + COALESCE(c.U2,0) + COALESCE(c.U3,0) + COALESCE(c.U4,0)) = 0
      THEN '0%'
    ELSE CONCAT(ROUND(COALESCE(c.U1,0) / (COALESCE(c.U1,0)+COALESCE(c.U2,0)+COALESCE(c.U3,0)+COALESCE(c.U4,0)) * 100, 2), '%')
  END AS ULC1,

  CASE 
    WHEN (COALESCE(c.U1,0) + COALESCE(c.U2,0) + COALESCE(c.U3,0) + COALESCE(c.U4,0)) = 0
      THEN '0%'
    ELSE CONCAT(ROUND(COALESCE(c.U2,0) / (COALESCE(c.U1,0)+COALESCE(c.U2,0)+COALESCE(c.U3,0)+COALESCE(c.U4,0)) * 100, 2), '%')
  END AS ULC2,

  CASE 
    WHEN (COALESCE(c.U1,0) + COALESCE(c.U2,0) + COALESCE(c.U3,0) + COALESCE(c.U4,0)) = 0
      THEN '0%'
    ELSE CONCAT(ROUND(COALESCE(c.U3,0) / (COALESCE(c.U1,0)+COALESCE(c.U2,0)+COALESCE(c.U3,0)+COALESCE(c.U4,0)) * 100, 2), '%')
  END AS ULC3,

  CASE 
    WHEN (COALESCE(c.U1,0) + COALESCE(c.U2,0) + COALESCE(c.U3,0) + COALESCE(c.U4,0)) = 0
      THEN '0%'
    ELSE CONCAT(ROUND(COALESCE(c.U4,0) / (COALESCE(c.U1,0)+COALESCE(c.U2,0)+COALESCE(c.U3,0)+COALESCE(c.U4,0)) * 100, 2), '%')
  END AS ULC4

FROM (
  SELECT
    SUM(ulc1 = 1) AS U1,
    SUM(ulc2 = 1) AS U2,
    SUM(ulc3 = 1) AS U3,
    SUM(ulc4 = 1) AS U4
  FROM dumper_cycles
  WHERE t_stamp BETWEEN :starttime AND :endtime
) AS c;
