'View'(不删除)从连接获得的 Postgresql table 的重复行

'View' (NOT DELETE) Duplicate Rows from a Postgresql table obtained from joins

所以我有临时 table 我通过连接三个 table 创建的 :

  1. 行程
  2. 停靠
  3. Stop_times

Stop_times table 有一个 trip_ids 的列表,相应的停靠站以及这些停靠站的公交车预定到达和离开时间。

我在网上搜索过,似乎到处都能找到有关如何删除重复项(使用 ctid、嵌套查询)但无法查看的答案。

我的查询看起来像这样:

CREATE TEMP TABLE temp as
SELECT 
 (CASE st.arrival_time < current_timestamp::time     
 WHEN true THEN (current_timestamp::date + interval '1 day') + st.arrival_time     
 ELSE (current_timestamp::date) + st.arrival_time     
 END) as arrival,      
 CASE st.departure_time < current_timestamp::time     
 WHEN true THEN (current_timestamp::date + interval '1 day') + st.departure_time     
 ELSE (current_timestamp::date) + st.departure_time     
 END as departure,     st.trip_id, st.stop_id, st.stop_headsign,route_id,   t.trip_headsign, s.stop_code, s.stop_name,      s.stop_lat, s.stop_lon

 FROM schema.stop_times st     
 JOIN schema.trips t ON t.trip_id=st.trip_id     
 JOIN schema.stops s ON s.stop_id=st.stop_id

 order by arrival, departure;  

我知道有重复项(通过 运行 select * 和 select DISTINCT on temp),我只需要识别重复项...任何帮助都会赞赏!

PS :我知道我可以使用 DISTINCT 并删除重复项,但它大大减慢了查询速度所以我需要重新处理我需要识别重复项的查询,结果记录大于 200,000,因此将它们导出到 excel 并过滤重复项也不是一个选项(我试过但 excel 无法处理)

我相信这会给你想要的:

SELECT arrival, departure, trip_id, stop_id, stop_headsign, route_id,
headsign, stop_code, stop_name, stop_lat, stop_lon, count(*)
FROM temp
GROUP BY arrival, departure, trip_id, stop_id, stop_headsign, route_id,
headsign, stop_code, stop_name, stop_lat, stop_lon
HAVING count(*) > 1;