 -- Find out-of-order stops for the most recent feed added.
 -- Change the first subquery to SELECT different feeds.
 \C 'Out-of-order stop geometries'
-WITH
-  f AS (SELECT MAX(feed_index) AS feed_index FROM :schema.feed_info),
-  d AS (
+WITH f AS (SELECT MAX(feed_index) AS feed_index FROM :schema.feed_info)
+SELECT feed_index,
+  trip_id,
+  route_id,
+  stop_id,
+  stop_sequence,
+  lag::numeric(9, 3),
+  dist::numeric(9, 3), 
+  coalesce(lead, length)::numeric(9, 3) as lead,
+  length::numeric(9, 3)
+FROM (
     SELECT
       feed_index,
       trip_id,

\C 'Possible foreign key violations'

WITH v AS (
  SELECT 'route_types_fkey' as constraint, count(distinct (feed_index, route_type)) as v
    FROM :schema.routes a
      LEFT JOIN :schema.route_types b USING (route_type)
    WHERE b.description IS NULL

    FROM :schema.stop_times
      INNER JOIN f USING (feed_index)
    WINDOW trip AS (PARTITION BY feed_index, trip_id ORDER BY stop_sequence)
  ) AS d
  LEFT JOIN :schema.trips trip USING (feed_index, trip_id)
  LEFT JOIN :schema.shape_geoms shape USING (feed_index, shape_id)
WHERE COALESCE(lead, length) > lag
  AND (dist > COALESCE(lead, length) OR dist < lag);
