2016-07-27 3 views
0

UNION을 사용하여 두 개의 뷰 (보기 CSP에 열이 하나 더 포함되어 있으므로 두 번째보기의 일부 항목이 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을 사용하십시오.

두 번째로 두 번째 필터를 제거하십시오. 여기 not exists 사용하는 방법입니다 :

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

당신은이 쿼리를 사용할 수는

SELECT spc.customer_no, 
    spc.contract, 
    spc.customer_part_no, 
    csp.configuration_id, 
    spc.catalog_no FROM superproduct spc 
    LEFT JOIN customersomething csp ON spc.customer_no = csp.customer_no 
    UNION ALL 
    SELECT csp.customer_no, 
    csp.contract, 
    csp.customer_part_no, 
    csp.configuration_id, 
    csp.catalog_no 
    FROM customersomething csp 
    LEFT JOIN superproduct spc ON spc.customer_no = csp.customer_no AND spc.customer_no IS NULL 
관련 문제