2012-03-18 59 views
0

我在Windows 7 x64上運行Postgres 9.1.3 32位。 (已使用32位因爲沒有Windows的PostGIS版本與64個Postgres的兼容。)(編輯:作爲PostGIS的2.0,它是與Windows Postgres的64位兼容)Postgres LEFT JOIN正在創建比左表更多的行

我有一個查詢, left將表(consistent.master)與臨時表結合,然後將結果數據插入第三個表(consistent.masternew)。

由於這是一個left join,因此生成的表應該具有與查詢中左表相同數量的行。但是,如果我運行這個:

SELECT count(*) 
FROM consistent.master 

我得到2085343。但如果我運行這個:

SELECT count(*) 
FROM consistent.masternew 

我得到2085703

masternew怎麼能有比master更多的行?不應該masternew具有與master相同的行數,查詢中的左表是?

以下是查詢。 mastermasternew表應該具有相同的結構。

--temporary table created here 
--I am trying to locate where multiple tickets were written on 
--a single traffic stop 
WITH stops AS (
    SELECT citation_id, 
      rank() OVER (ORDER BY offense_timestamp, 
        defendant_dl, 
        offense_street_number, 
        offense_street_name) AS stop 
    FROM consistent.master 
    WHERE citing_jurisdiction=1 
) 

--Here's the insert statement. Below you'll see it's 
--pulling data from a select query 
INSERT INTO consistent.masternew (arrest_id, 
    citation_id, 
    defendant_dl, 
    defendant_dl_state, 
    defendant_zip, 
    defendant_race, 
    defendant_sex, 
    defendant_dob, 
    vehicle_licenseplate, 
    vehicle_licenseplate_state, 
    vehicle_registration_expiration_date, 
    vehicle_year, 
    vehicle_make, 
    vehicle_model, 
    vehicle_color, 
    offense_timestamp, 
    offense_street_number, 
    offense_street_name, 
    offense_crossstreet_number, 
    offense_crossstreet_name, 
    offense_county, 
    officer_id, 
    offense_code, 
    speed_alleged, 
    speed_limit, 
    work_zone, 
    school_zone, 
    offense_location, 
    source, 
    citing_jurisdiction, 
    the_geom) 

--Here's the select query that the insert statement is using.  
SELECT stops.stop, 
    master.citation_id, 
    defendant_dl, 
    defendant_dl_state, 
    defendant_zip, 
    defendant_race, 
    defendant_sex, 
    defendant_dob, 
    vehicle_licenseplate, 
    vehicle_licenseplate_state, 
    vehicle_registration_expiration_date, 
    vehicle_year, 
    vehicle_make, 
    vehicle_model, 
    vehicle_color, 
    offense_timestamp, 
    offense_street_number, 
    offense_street_name, 
    offense_crossstreet_number, 
    offense_crossstreet_name, 
    offense_county, 
    officer_id, 
    offense_code, 
    speed_alleged, 
    speed_limit, 
    work_zone, 
    school_zone, 
    offense_location, 
    source, 
    citing_jurisdiction, 
    the_geom 
FROM consistent.master LEFT JOIN stops 
ON stops.citation_id = master.citation_id 

在它的事項的情況下,我已經運行VACUUM FULL ANALYZE和重建索引兩個表。 (不清楚確切的命令;通過pgAdmin III完成。)

回答

7

左連接不一定具有與左表中行數相同的行數。基本上,它就像一個普通的連接,除了左表中不會出現在普通連接中的行也被添加。因此,如果右表中有多行與左表中的一行匹配,則結果中的行數可能會多於左表的行數。

爲了做你想做的事情,你應該使用group by和count來檢測倍數。

select citation_id 
from stops join master on stops.citation_id = master.citation_id 
group by citation_id 
having count(*) > 1 
+0

謝謝。就是這樣。 'LEFT JOIN'意味着連接中的*最小*行數將是左表的數量。 – 2012-03-19 02:22:27