2012-03-18 4 views
0

Windows 7 x64上でPostgres 9.1.3 32ビットを実行しています。 (64ビットのPostgresとの互換性なしのWindows PostGISのリリースが存在しないため、32ビットを使用する必要があります。)(EDIT:PostGISの2.0の時点で、それはWindows上でPostgresの64ビットと互換性があります)をPostgres LEFT JOINが左テーブルより多くの行を作成しています

私はそのクエリを持っていますは、テンポラリテーブルとテーブル(consistent.master)を結合し、結果のデータを第3のテーブル(consistent.masternew)に挿入します。

これはleft joinなので、結果として得られるテーブルは、クエリの左側のテーブルと同じ数の行を持つ必要があります。私が実行している場合しかし、この:

SELECT count(*) 
FROM consistent.master 

私は2085343を取得します。しかし、私がこれを実行した場合:を得る。

masternewにはmasterよりも多くの行がありますか? masternewは、masterと同じ行数を持つべきではありません。クエリの左側のテーブルは?

以下はクエリです。テーブルmasterとテーブルは同じ構造でなければなりません。

--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を実行し、両方のテーブルにインデックスを再作成しています。

答えて

7

左結合は、必ずしも左のテーブルの行数と同じ数の行を持つとは限りません(正確なコマンドは不明ですが、pgAdmin IIIで実行しました)。基本的には、通常の結合に似ていますが、通常の結合では表示されない左側の表の行も追加されます。したがって、左側の表の1つの行に一致する複数の行が右側の表にある場合、左側の表の行の数より多くの行を結果に含めることができます。

あなたがしたいことをするためには、グループを使用し、複数を検出するにはカウントを使用する必要があります。

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

ありがとうございます。それはまさにそれです。 LEFT JOINは、結合の行の最小数*が左の表の行の数になることを意味します。 –

関連する問題