2016-07-27 4 views
0

UNIONを使用して2つのビューを接続したい(ビューCSPには1列が含まれていますので、2番目のビューの項目が1番目のビューにない場合は2番目の*を使用したい)良いですが、正しい値と*のどちらかを重複した設定IDがありません。別の列を持つ連合

これを解決し、cspに値があるときに '*'で行を削除するにはどうすればいいですか?

SELECT csp.customer_no, 
     csp.contract, 
     csp.customer_part_no, 
     csp.configuration_id, 
     csp.catalog_no 
FROM customersomething csp 
UNION 
SELECT spc.customer_no, 
     spc.contract, 
     spc.customer_part_no, 
     '*' AS "configuration_id", 
     spc.catalog_no 
FROM 
superproduct spc 

+-------------+----------+-----+------------------+--------+ 
| customer_no | contract | ... | configuration_id |  | 
+-------------+----------+-----+------------------+--------+ 
|   17 | whatever | ... | *    | view A | 
|   17 | whatever | ... | right_one  | view B | 
+-------------+----------+-----+------------------+--------+ 

答えて

0

最初に、重複を削除するオーバーヘッドが発生しない限り、union allを使用してください。

第2に、第2のものを除外します。 、あなたがこのクエリを使用することができ

SELECT csp.customer_no, csp.contract, csp.customer_part_no, 
     csp.configuration_id, csp.catalog_no 
FROM customersomething csp 
UNION ALL 
SELECT spc.customer_no, spc.contract, spc.customer_part_no, 
     '*' AS "configuration_id", spc.catalog_no 
FROM superproduct spc 
WHERE NOT EXISTS (SELECT 1 
        FROM customersomething csp 
        WHERE scp.customer_no = spc.customer_no 
       ); 
0

:ここ not existsを使用しての方法です
関連する問題