我的查询类似于:
select userid, access, childaccess from table 1
union all
select userid, access, childaccess from table 2
返回一组看起来像的数据:
JSMITH - Admin - View1
JSMITH - Admin - View2
JSMITH - Controls - NULL
JSMITH - View1 - NULL
JSMITH - View2 - NULL
我需要比较数据并删除Access和ChildAccess列之间的任何重复项,以便上面的数据如下所示:
JSMITH - Admin - View1
JSMITH - Admin - View2
JSMITH - Controls - NULL
因此,我们将删除Access项目等于ChildAccess项目的结果。
答案 0 :(得分:0)
使用:
WITH cte AS
( select userid, access, childaccess
from table_1
union
select userid, access, childaccess
from table_2
)
SELECT *
FROM cte c
WHERE NOT EXISTS (SELECT 1
FROM cte c2
WHERE c2.userid = c.userid
AND c2.childaccess = c.access);