WITH combined_jams AS (
    SELECT Name, t_stamp
    FROM jam_area
    WHERE t_stamp BETWEEN :starttime AND :endtime
    
    UNION ALL
    
    SELECT Name, t_stamp
    FROM lane_data
    WHERE t_stamp BETWEEN :starttime AND :endtime
      AND Jam = 1
),
jam_counts AS (
    SELECT
        Name,
        COUNT(*) AS Jam_count
    FROM combined_jams
    GROUP BY Name
),
total AS (
    SELECT SUM(Jam_count) AS Total_jams FROM jam_counts
)
SELECT
    j.Name,
    ROUND((j.Jam_count / t.Total_jams) * 100, 2) AS Jam_percentage
FROM jam_counts j
CROSS JOIN total t
ORDER BY Jam_percentage DESC;
