SELECT
    Name,
    COUNT(*) AS Jam_count
FROM (
    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
) AS combined_jams
GROUP BY Name
ORDER BY Jam_count DESC;
