我的子查询产生如下结果:
coulmn1 column2
a d
b z1000
c c
d
1
2
z1000 k
我想知道两组中的不同元素。 column1 = {a,b,c,1,2,d,z1000,.....}第2列= {d,c,z1000,k ......}我想要的结果是= {a, k,1,2,....}希望我说清楚..请让我知道我该怎么做..?
答案 0 :(得分:1)
--Test Data
with temp_table as (
select 'a' coulmn1,'b' column2 union all
select 'b' coulmn1,'z1000' column2 union all
select 'c' coulmn1,'c' column2 union all
select 'd' coulmn1,'' column2 union all
select 'z1000' coulmn1,'k' column2
)
--use cross join and union to distinct data
--you have to change temp_table to your own table
select * into #temp_table from (
select T.coulmn1,T2.column2 as column2
from temp_table T,temp_table T2
where T.coulmn1 <> T2.column2
) T;
select coulmn1 from #temp_table
union
select column2 from #temp_table;
答案 1 :(得分:0)
一种方法是全外连接:
select coalesce(t1.col1, t2.col2)
from t t1 full join
t t2
on t1.col1 = t2.col2
where t1.col1 is null or t2.col2 is null;
另一种方法不需要运行子查询两次;
select v.col
from t cross apply
(values (t.col1, 1), (t.col2, 2)) v(col, which)
group by v.col
having min(v.which) = max(v.which);
答案 2 :(得分:0)
使用 EXCEPT 。
SELECT column1
FROM your_subquery
EXCEPT
SELECT column2
FROM your_subquery
UNION
SELECT column2
FROM your_subquery
EXCEPT
SELECT column1
FROM your_subquery