WITH counts AS (
  SELECT
    CAST(COALESCE(SUM(ulc1=1),0) AS SIGNED) AS ULC1,
    CAST(COALESCE(SUM(ulc2=1),0) AS SIGNED) AS ULC2,
    CAST(COALESCE(SUM(ulc3=1),0) AS SIGNED) AS ULC3,
    CAST(COALESCE(SUM(ulc4=1),0) AS SIGNED) AS ULC4
  FROM dumper_cycles
  WHERE t_stamp BETWEEN :starttime AND :endtime
),
totals AS (
  SELECT (ULC1+ULC2+ULC3+ULC4) AS Dump_Total FROM counts
),
labels AS (
  SELECT 'ULC1' AS Dumper UNION ALL
  SELECT 'ULC2' UNION ALL
  SELECT 'ULC3' UNION ALL
  SELECT 'ULC4'
)
SELECT
  l.Dumper,
  CASE WHEN l.Dumper='ULC1' THEN c.ULC1 END AS ULC1,
  CASE WHEN l.Dumper='ULC2' THEN c.ULC2 END AS ULC2,
  CASE WHEN l.Dumper='ULC3' THEN c.ULC3 END AS ULC3,
  CASE WHEN l.Dumper='ULC4' THEN c.ULC4 END AS ULC4,
  t.Dump_Total AS Dump_Count
FROM labels l
CROSS JOIN counts c
CROSS JOIN totals t;
