I have two tables with the same schema:
dbo.orders_1
| user_id | order_id | create_time |
|---------|----------|-------------------------|
| 1 | 5 | 2018-05-04 02:26:03.808 |
| 1 | 6 | 2021-01-01 02:26:03.821 |
| 2 | 12 | 2021-12-12 02:26:03.814 |
dbo.orders_2
| user_id | order_id | create_time |
|---------|----------|-------------------------|
| 6 | 7 | 2020-06-04 02:26:03.808 |
| 1 | 5 | 2021-01-01 02:26:03.821 |
| 3 | 4 | 2021-12-12 02:26:03.814 |
This is how I can find duplicated rows between two tables:
select
a.user_id,
a.order_id,
a.create_time as "create_time_1",
b.create_time as "create_time_2"
from
orders_1 a
join orders_2 b on a.user_id = b.user_id
and a.order_id = b.order_id
Which gives me this output:
| user_id | order_id | create_time_1 | create_time_2 |
|---------|----------|-------------------------|-------------------------|
| 1 | 5 | 2020-06-04 02:26:03.808 | 2021-01-01 02:26:03.821 |
It works, but now I added another table:
dbo.orders_3
| user_id | order_id | create_time |
|---------|----------|-------------------------|
| 77 | 2 | 2015-09-15 02:26:03.808 |
| 3 | 4 | 2018-04-15 02:26:03.814 |
I want to find any duplicates in those 3 tables. What I expect is:
| user_id | order_id | create_time_1 | create_time_2 | create_time_3 |
|---------|----------|-------------------------|-------------------------|-------------------------|
| 1 | 5 | 2020-06-04 02:26:03.808 | 2021-01-01 02:26:03.821 | NULL |
| 3 | 4 | NULL | 2021-12-12 02:26:03.814 | 2018-04-15 02:26:03.814 |
What should sql look like? This is my online playground: https://sqlize.online/sql/mssql2019/7f31a47ef4c633ad8b4ba88a4276964d/
user_id
andorder_id
orcreate_time
as well?user_id
andorder_id
is a duplicate in this case.create_time
doesn't matter