2017-12-08 4 views
0

重複しているか、左端の結合を使用しているのが発生しています。左の結合を使用してレコードを無効にする

Salesは、数多くのブランドを持っているが、唯一異なるpricingsと同じブランド名の2:BRAND 2

CSR Productには2つのBRAND 2ブランドがありますが、それぞれ異なる州です。

Product State Packageには2つがありませんBRAND 2です。

私はbrandフィールドでこれらの3つを結合していますが、まだそれぞれが表示されていますBRAND 2テーブルに追加するとサイズが増えます。これをどうすれば解決できますか?

私はすべてSalesレコードが必要なので、なぜ私はセールスに加わりましたか?これは、最初の左結合が3020レコードであり、2番目の左結合が450000レコードであったところで直面しているより大きな問題の小さな例です。

私の質問は:私はすべての売上レコードを取得するためにLEFT JOIN INGだなら、なぜ他の二つの結合されたテーブルにBRAND 2を複製追加すると、販売記録を掛けていますか?

販売:

brand | pricing 
---------------- 
BRAND 1 1 
BRAND 2 2 <--- Brand 2 
BRAND 2 7 <--- Brand 2 
BRAND 3 3 
BRAND 4 4 
BRAND 5 5 
BRAND 6 6 

製品の状態パッケージ:

brand | state 
---------------- 
BRAND 5 WA 
BRAND 4 AZ 
BRAND 2 MO 
BRAND 1 VT 
BRAND 8 SD 

CSR製品:

brand | state 
---------------- 
BRAND 2 CT <--- Brand 2 
BRAND 3 MA 
BRAND 2 CA <--- Brand 2 
BRAND 4 GA 
BRAND 5 LA 
BRAND 9 WA 

問合せ:

SELECT s.brand, 
     tcp.brand as csr_brand, 
     tpsp.brand as psp_brand 
FROM test_sales s 
     LEFT JOIN test_csr_product tcp 
       ON (s.brand = tcp.brand) 
     LEFT JOIN test_product_state_package tpsp 
       ON (s.brand = tpsp.brand); 

出力:Salesで3 BRAND 2

-- Need all sales records 
sales_brand | csr_brand | psp_brand 
-------------+-----------+----------- 
BRAND 1  |   | BRAND 1 
BRAND 2  | BRAND 2 | BRAND 2 
BRAND 2  | BRAND 2 | BRAND 2 
BRAND 2  | BRAND 2 | BRAND 2 
BRAND 2  | BRAND 2 | BRAND 2 
BRAND 3  | BRAND 3 | 
BRAND 4  | BRAND 4 | BRAND 4 
BRAND 5  | BRAND 5 | BRAND 5 
BRAND 6  |   | 
(9 rows) 

:あなたが売上全体sale_priceための重複があります見ることができます:それは

sales_brand | sale_price | csr_brand | psp_brand 
-------------+------------+-----------+----------- 
BRAND 1  |   1 |   | BRAND 1 
BRAND 2  |   2 | BRAND 2 | BRAND 2 
BRAND 2  |   2 | BRAND 2 | BRAND 2 
BRAND 2  |   7 | BRAND 2 | BRAND 2 
BRAND 2  |   7 | BRAND 2 | BRAND 2 
BRAND 2  |   10 | BRAND 2 | BRAND 2 
BRAND 2  |   10 | BRAND 2 | BRAND 2 
BRAND 3  |   3 | BRAND 3 | 
BRAND 4  |   4 | BRAND 4 | BRAND 4 
BRAND 5  |   5 | BRAND 5 | BRAND 5 
BRAND 6  |   6 |   | 
(11 rows) 
+0

あなたの希望する出力の例を書いてもいいですか? –

答えて

0

にフィットしていますか?私は正確にヨール問題を理解しているが、あなたはその結果にtpsp.state、tcp.stateを考慮すれば、結果行が実際に複製されていない場合はわからない

SELECT s.brand, 
     tcp.brand as csr_brand, 
     tpsp.brand as psp_brand 
FROM (select distinct t.brand from test_sales t) s 
     LEFT JOIN test_csr_product tcp 
       ON (s.brand = tcp.brand) 
     LEFT JOIN test_product_state_package tpsp 
       ON (s.brand = tpsp.brand); 
+0

私の質問は 'DISTINCT'レコードに関するものではないからです。なぜなら、LEFT JOINされたテーブル内の同じ「BRAND 2」の値( 'brand'はテーブルが結合されているカラム)を追加して、' sales'テーブルに重複を生じさせる理由です。 「DISTINCT」は、「BRAND 2」で10の売上高があっても、「BRAND 2」の「10」の1つの販売価格のみを表示する。私はすべての販売が必要 – Growler

+0

あなたは希望の出力の例を挙げることができますか? –

関連する問題