WITH counts AS (
  SELECT
    CAST(COALESCE(SUM(Fluid_Inbound_Merge_1 = 1), 0) AS SIGNED) AS Fluid_Inbound_Merge_1,
    CAST(COALESCE(SUM(Fluid_Inbound_Merge_2 = 1), 0) AS SIGNED) AS Fluid_Inbound_Merge_2,
    CAST(COALESCE(SUM(Fluid_Inbound_Merge_3 = 1), 0) AS SIGNED) AS Fluid_Inbound_Merge_3,
    CAST(COALESCE(SUM(Fluid_Inbound_Merge_4 = 1), 0) AS SIGNED) AS Fluid_Inbound_Merge_4,
    CAST(COALESCE(SUM(Fluid_Inbound_Merge_5 = 1), 0) AS SIGNED) AS Fluid_Inbound_Merge_5,
    CAST(COALESCE(SUM(Fluid_Inbound_Merge_6 = 1), 0) AS SIGNED) AS Fluid_Inbound_Merge_6,
    CAST(COALESCE(SUM(Fluid_Inbound_Merge_7 = 1), 0) AS SIGNED) AS Fluid_Inbound_Merge_7,
    CAST(COALESCE(SUM(Non_Con_Merge = 1), 0) AS SIGNED) AS Non_Con_Merge,
    CAST(COALESCE(SUM(NCP = 1), 0) AS SIGNED) AS NCP,
    CAST(COALESCE(SUM(Bulk_Inbound_MCM04 = 1), 0) AS SIGNED) AS Bulk_Inbound_MCM04,
    CAST(COALESCE(SUM(Fluid_Outbound_MCM04 = 1), 0) AS SIGNED) AS Fluid_Outbound_MCM04,
    CAST(COALESCE(SUM(Problem_Solve_MCM04 = 1), 0) AS SIGNED) AS Problem_Solve_MCM04,
    CAST(COALESCE(SUM(Bulk_Inbound_MCM05 = 1), 0) AS SIGNED) AS Bulk_Inbound_MCM05,
    CAST(COALESCE(SUM(Fluid_Outbound_MCM05 = 1), 0) AS SIGNED) AS Fluid_Outbound_MCM05,
    CAST(COALESCE(SUM(Problem_Solve_MCM05 = 1), 0) AS SIGNED) AS Problem_Solve_MCM05,
    CAST(COALESCE(SUM(NCS1 = 1), 0) AS SIGNED) AS NCS1,
    CAST(COALESCE(SUM(NCS2 = 1), 0) AS SIGNED) AS NCS2,
    CAST(COALESCE(SUM(BYB = 1), 0) AS SIGNED) AS BYB,
    CAST(COALESCE(SUM(BYA = 1), 0) AS SIGNED) AS BYA,
    CAST(COALESCE(SUM(BYC = 1), 0) AS SIGNED) AS BYC,
    CAST(COALESCE(SUM(BYD = 1), 0) AS SIGNED) AS BYD,
    CAST(COALESCE(SUM(Chutes_MCM04 = 1), 0) AS SIGNED) AS Chutes_MCM04,
    CAST(COALESCE(SUM(Chutes_MCM05 = 1), 0) AS SIGNED) AS Chutes_MCM05
  FROM jam_area
  WHERE t_stamp BETWEEN :starttime AND :endtime
),
totals AS (
  SELECT (Fluid_Inbound_Merge_1 + Fluid_Inbound_Merge_2 + Fluid_Inbound_Merge_3 + Fluid_Inbound_Merge_4 + 
          Fluid_Inbound_Merge_5 + Fluid_Inbound_Merge_6 + Fluid_Inbound_Merge_7 + Non_Con_Merge + NCP + 
          Bulk_Inbound_MCM04 + Fluid_Outbound_MCM04 + Problem_Solve_MCM04 + Bulk_Inbound_MCM05 + 
          Fluid_Outbound_MCM05 + Problem_Solve_MCM05 + NCS1 + NCS2 + BYB + BYA + BYC + BYD + 
          Chutes_MCM04 + Chutes_MCM05) AS Jam_Total
  FROM counts
),
labels AS (
  SELECT 'Fluid_Inbound_Merge_1' AS Area UNION ALL
  SELECT 'Fluid_Inbound_Merge_2' UNION ALL
  SELECT 'Fluid_Inbound_Merge_3' UNION ALL
  SELECT 'Fluid_Inbound_Merge_4' UNION ALL
  SELECT 'Fluid_Inbound_Merge_5' UNION ALL
  SELECT 'Fluid_Inbound_Merge_6' UNION ALL
  SELECT 'Fluid_Inbound_Merge_7' UNION ALL
  SELECT 'Non_Con_Merge' UNION ALL
  SELECT 'NCP' UNION ALL
  SELECT 'Bulk_Inbound_MCM04' UNION ALL
  SELECT 'Fluid_Outbound_MCM04' UNION ALL
  SELECT 'Problem_Solve_MCM04' UNION ALL
  SELECT 'Bulk_Inbound_MCM05' UNION ALL
  SELECT 'Fluid_Outbound_MCM05' UNION ALL
  SELECT 'Problem_Solve_MCM05' UNION ALL
  SELECT 'NCS1' UNION ALL
  SELECT 'NCS2' UNION ALL
  SELECT 'BYB' UNION ALL
  SELECT 'BYA' UNION ALL
  SELECT 'BYC' UNION ALL
  SELECT 'BYD' UNION ALL
  SELECT 'Chutes_MCM04' UNION ALL
  SELECT 'Chutes_MCM05'
)
SELECT
  l.Area,
  CASE WHEN l.Area = 'Fluid_Inbound_Merge_1' THEN c.Fluid_Inbound_Merge_1 END AS `Fluid_Inbound_Merge_1`,
  CASE WHEN l.Area = 'Fluid_Inbound_Merge_2' THEN c.Fluid_Inbound_Merge_2 END AS `Fluid_Inbound_Merge_2`,
  CASE WHEN l.Area = 'Fluid_Inbound_Merge_3' THEN c.Fluid_Inbound_Merge_3 END AS `Fluid_Inbound_Merge_3`,
  CASE WHEN l.Area = 'Fluid_Inbound_Merge_4' THEN c.Fluid_Inbound_Merge_4 END AS `Fluid_Inbound_Merge_4`,
  CASE WHEN l.Area = 'Fluid_Inbound_Merge_5' THEN c.Fluid_Inbound_Merge_5 END AS `Fluid_Inbound_Merge_5`,
  CASE WHEN l.Area = 'Fluid_Inbound_Merge_6' THEN c.Fluid_Inbound_Merge_6 END AS `Fluid_Inbound_Merge_6`,
  CASE WHEN l.Area = 'Fluid_Inbound_Merge_7' THEN c.Fluid_Inbound_Merge_7 END AS `Fluid_Inbound_Merge_7`,
  CASE WHEN l.Area = 'Non_Con_Merge' THEN c.Non_Con_Merge END AS `Non_Con_Merge`,
  CASE WHEN l.Area = 'NCP' THEN c.NCP END AS `NCP`,
  CASE WHEN l.Area = 'Bulk_Inbound_MCM04' THEN c.Bulk_Inbound_MCM04 END AS `Bulk_Inbound_MCM04`,
  CASE WHEN l.Area = 'Fluid_Outbound_MCM04' THEN c.Fluid_Outbound_MCM04 END AS `Fluid_Outbound_MCM04`,
  CASE WHEN l.Area = 'Problem_Solve_MCM04' THEN c.Problem_Solve_MCM04 END AS `Problem_Solve_MCM04`,
  CASE WHEN l.Area = 'Bulk_Inbound_MCM05' THEN c.Bulk_Inbound_MCM05 END AS `Bulk_Inbound_MCM05`,
  CASE WHEN l.Area = 'Fluid_Outbound_MCM05' THEN c.Fluid_Outbound_MCM05 END AS `Fluid_Outbound_MCM05`,
  CASE WHEN l.Area = 'Problem_Solve_MCM05' THEN c.Problem_Solve_MCM05 END AS `Problem_Solve_MCM05`,
  CASE WHEN l.Area = 'NCS1' THEN c.NCS1 END AS `NCS1`,
  CASE WHEN l.Area = 'NCS2' THEN c.NCS2 END AS `NCS2`,
  CASE WHEN l.Area = 'BYB' THEN c.BYB END AS `BYB`,
  CASE WHEN l.Area = 'BYA' THEN c.BYA END AS `BYA`,
  CASE WHEN l.Area = 'BYC' THEN c.BYC END AS `BYC`,
  CASE WHEN l.Area = 'BYD' THEN c.BYD END AS `BYD`,
  CASE WHEN l.Area = 'Chutes_MCM04' THEN c.Chutes_MCM04 END AS `Chutes_MCM04`,
  CASE WHEN l.Area = 'Chutes_MCM05' THEN c.Chutes_MCM05 END AS `Chutes_MCM05`,
  t.Jam_Total AS `Jam Count`
FROM labels l
CROSS JOIN counts c
CROSS JOIN totals t;
