     FROM :schema.transfers a
       LEFT JOIN :schema.calendar b USING (feed_index, service_id)
     WHERE coalesce(b.monday, b.friday) IS NULL
-) SELECT v.constraint, v as "possible violations" FROM v WHERE v.v > 0;
+) AS v
+WHERE count > 0;
 
 \C 'Non-NULL constraint violations'
 SELECT DISTINCT NULL::int feed_index, 'routes' as table, 'route_type' as key, a.route_type::text as value

    FROM :schema.stop_times
      INNER JOIN f USING (feed_index)
    WINDOW trip AS (PARTITION BY feed_index, trip_id ORDER BY stop_sequence)
  )
  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 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);

\C 'Possible foreign key violations'

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