2015-03-30 58 views
0

所以我臨時表,我通過連接三個表創建:'查看'(NOT DELETE)從獲得PostgreSQL表重複的行加入

  1. 旅行
  2. 停止
  3. Stop_times

Stop_times表具有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 *並選擇溫度是不同的),我只需要找出重複...任何幫助會不勝感激! PS:我知道我可以使用DISTINCT並刪除重複項,但它會減慢查詢的速度,所以我需要重新進行查詢,以便我需要識別重複項,結果記錄大於200,000所以它們導出到Excel和過濾重複不是一個選項是(我試過,但Excel不能處理它)

回答

0

我相信這會給你想要的東西:

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; 
+0

謝謝!用「HAVING count(*)> 1」,它工作:) – dahvyah 2015-03-30 19:34:14

+0

不客氣。您會選擇答案旁邊的複選標記按鈕,以表明您選擇/接受此答案嗎? – 2015-03-30 19:36:20