Another way you can accomplish thisyour goal is just FULL JOIN
ing all of the Tables involved, and using a WHERE
clause to filter out anything that isn't a duplicate like so:
SELECT
COALESCE(a.user_id, b.user_id, c.user_id) AS user_id,
COALESCE(a.order_id, b.order_id, c.order_id) AS order_id,
a.create_time AS create_time1,
b.create_time AS create_time2,
ISNULL(c.create_time, d.create_time) AS create_time3
FROM dbo.orders_1 AS a
FULL JOIN dbo.orders_2 AS b
ON a.user_id = b.user_id
AND a.order_id = b.order_id
FULL JOIN dbo.orders_3 AS c
ON a.user_id = c.user_id
AND a.order_id = c.order_id
FULL JOIN dbo.orders_3 AS d
ON b.user_id = d.user_id
AND b.order_id = d.order_id
WHERE
(a.user_id IS NOT NULL AND b.user_id IS NOT NULL) -- A duplicate between orders_1 and orders_2
OR (a.user_id IS NOT NULL AND c.user_id IS NOT NULL) -- A duplicate between orders_1 and orders_3
OR (b.user_id IS NOT NULL AND d.user_id IS NOT NULL) -- A duplicate between orders_2 and orders_3
Notice there are 3 FULL JOIN
s, one for each Table comparison, i.e.: orders_1
to orders_2
, orders_1
to orders_3
and orders_2
to orders_3
.