联盟有一个不同的专栏

时间:2016-07-27 11:26:19

标签: sql oracle union

我想使用UNION来连接两个视图(查看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 |
+-------------+----------+-----+------------------+--------+

2 个答案:

答案 0 :(得分: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
                 );

答案 1 :(得分:0)

您可以使用此查询

IsWindowPatternAvailable