WITH
select_1 AS (
SELECT *
FROM your_table
WHERE your_condition = 1
),
select_2 AS (
SELECT *
FROM your_other_table
WHERE your_other_condition = 1
)
SELECT * FROM select_1
UNION
SELECT * FROM select_2
MINUS
(
SELECT * FROM select_1
INTERSECT
SELECT * FROM select_2
);
(select table_name from dba_tables where user = 'X'
union
select table_name from dba_tables where user = 'Y')
minus
(select table_name from dba_tables where user = 'X'
intersect
select table_name from dba_tables where user = 'Y')
select KK1, KK2, NK1, NK2 from ( select KK1, KK2, NK1, NK2, count( * ) over( partition by KK1, KK2 ) cnt from ( select KK1, KK2, NK1, NK2 from X union all select KK1, KK2, NK1, NK2 from Y ) ) where cnt = 1;
我有2个数据集表1 ID名称 1 A B 3C 7 D 表2 ID名称 1 A 2 B 4 E 6 F(select * from table1 minus select * from table2)Union(select * from table2 minus select * from table1)
7条答案
按热度按时间ztigrdn81#
如果
select1
和select2
都没有返回重复项,则可以使用以下命令:bgtovc5b2#
在Oracle 10 g中,您可以随意使用公共表表达式。
这样可以使子查询保持可维护性,并使最终查询的目的清晰明了。
当然,让Oracle在SQL中添加一个
SYM_DIFFERENCE
运算符会更好,但我并没有屏住呼吸--他们仍然不相信BOOLEAN
数据类型是一个好主意。mklgxw1f3#
还有一个想法:
就像这样:
m2xkgtsf4#
这对我很有效-不确定它有多快。
vd8tlhqk5#
我来
dgsult0t6#
下面是另一个解决方案,这次使用count()分析(Oracle 10或更高版本)。
优点:
select KK1, KK2, NK1, NK2 from ( select KK1, KK2, NK1, NK2, count( * ) over( partition by KK1, KK2 ) cnt from ( select KK1, KK2, NK1, NK2 from X union all select KK1, KK2, NK1, NK2 from Y ) ) where cnt = 1;
8oomwypt7#
我有2个数据集表1
ID名称
1 A
B
3C
7 D
表2 ID名称
1 A 2 B 4 E 6 F(select * from table1 minus select * from table2)Union(select * from table2 minus select * from table1)