2016-07-27 82 views
0

我想使用UNION连接两个视图(查看CSP包含1个列,所以我想在第二个视图的某些项目的情况下使用*作为第二个不在第一个视图中),这是工作不错,但我没有重复的配置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

您可以使用此查询,