如何合并来自 2 个表的数据——哪个连接,什么条件?
How to combine data from 2 tables -- which join, what conditions?
考虑以下 2 tables.
表格DE
ID country key1 key2
------------------------
1 US 1 null
1 US 1 null
1 US 1 null
2 US null null
3 US 1 1
4 DE 1 1
5 DE null null
5 DE null null
TableUS
ID key1 key2
--------------
1 null null
2 null 1
4 1 1
8 null 1
2 null 1
2 null 1
9 1 null
我需要所有 ID 的清晰概览,结合来自 table 的数据:
ID inTableDe country DEkey1 DEkey2 inTableUS USkey1 USKey2
-----------------------------------------------------------------
1 1 US 1 0 1 0 0
2 1 US 0 0 1 0 1
3 1 US 1 1 0 0 0
4 1 DE 1 1 1 1 1
5 1 DE 0 0 0 0 0
8 0 0 0 1 1 0 1
9 0 0 0 1 1 1 0
我希望它不言自明:
ID 8
和 ID 9
在第一列中有 0
,因为它们不在 tableDE
中
ID 8
和 ID 9
在 country
列中有 0
,因为此字段在 tableUS
中不存在
ID 3
在 inTableUS
中有 0
,因为它只存在于 tableDE
key
值是从原始 tables 复制而来的
- 一个
ID
不是唯一的:它可以在两个table中出现很多次。但是:key1
和 key2
的值对于每个 ID
within 将始终相同 table.
我已经为此纠结了几个小时;我现在有这个:
select de.[ID],
de.[country],
case when (de.[ID] in (select distinct [ID] from [tableDE]) then 1 else 0 end as [inTableDE],
case when (de.[ID] in (select distinct [ID] from [tableUS]) then 1 else 0 end as [inTableUS],
de.[key1] as [DEKey1],
de.[key2] as [DEKey2],
us.[key1] as [USKey1],
us.[key2] as [USKey2],
from dbo.[tableDE] de
full outer join dbo.[tableUS] us on de.[ID] = us.[ID]
where de.[country] = 'US'
and (de.[key1] = 1 or de.[key2] = 1 or us.[key1] = 1 or us.[key2] = 1)
group by de.[ID], us.[ID]
但这一直只给我两个 tables 中的值。
我做错了什么?
您似乎想要在 full join
:
之上聚合
select
coalesce(de.id, us.id) as id,
case when de.id is null then 0 else 1 end as intablede,
max(de.country) as country,
coalesce(max(de.key1), 0) as dekey1,
coalesce(max(de.key2), 0) as dekey2,
case when us.id is null then 0 else 1 end as intableus,
coalesce(max(us.key1), 0) as uskey1,
coalesce(max(us.key2), 0) as uskey2
from dbo.tablede de
full join dbo.tableus us on de.id = us.id
group by de.id, us.id
order by id
id | intablede | country | dekey1 | dekey2 | intableus | uskey1 | uskey2
-: | --------: | :------ | -----: | -----: | --------: | -----: | -----:
1 | 1 | US | 1 | 0 | 1 | 0 | 0
2 | 1 | US | 0 | 0 | 1 | 0 | 1
3 | 1 | US | 1 | 1 | 0 | 0 | 0
4 | 1 | DE | 1 | 1 | 1 | 1 | 1
5 | 1 | DE | 0 | 0 | 0 | 0 | 0
8 | 0 | null | 0 | 0 | 1 | 0 | 1
9 | 0 | null | 0 | 0 | 1 | 1 | 0
考虑以下 2 tables.
表格DE
ID country key1 key2
------------------------
1 US 1 null
1 US 1 null
1 US 1 null
2 US null null
3 US 1 1
4 DE 1 1
5 DE null null
5 DE null null
TableUS
ID key1 key2
--------------
1 null null
2 null 1
4 1 1
8 null 1
2 null 1
2 null 1
9 1 null
我需要所有 ID 的清晰概览,结合来自 table 的数据:
ID inTableDe country DEkey1 DEkey2 inTableUS USkey1 USKey2
-----------------------------------------------------------------
1 1 US 1 0 1 0 0
2 1 US 0 0 1 0 1
3 1 US 1 1 0 0 0
4 1 DE 1 1 1 1 1
5 1 DE 0 0 0 0 0
8 0 0 0 1 1 0 1
9 0 0 0 1 1 1 0
我希望它不言自明:
ID 8
和ID 9
在第一列中有0
,因为它们不在tableDE
中
ID 8
和ID 9
在country
列中有0
,因为此字段在tableUS
中不存在
ID 3
在inTableUS
中有0
,因为它只存在于tableDE
key
值是从原始 tables 复制而来的
- 一个
ID
不是唯一的:它可以在两个table中出现很多次。但是:key1
和key2
的值对于每个ID
within 将始终相同 table.
我已经为此纠结了几个小时;我现在有这个:
select de.[ID],
de.[country],
case when (de.[ID] in (select distinct [ID] from [tableDE]) then 1 else 0 end as [inTableDE],
case when (de.[ID] in (select distinct [ID] from [tableUS]) then 1 else 0 end as [inTableUS],
de.[key1] as [DEKey1],
de.[key2] as [DEKey2],
us.[key1] as [USKey1],
us.[key2] as [USKey2],
from dbo.[tableDE] de
full outer join dbo.[tableUS] us on de.[ID] = us.[ID]
where de.[country] = 'US'
and (de.[key1] = 1 or de.[key2] = 1 or us.[key1] = 1 or us.[key2] = 1)
group by de.[ID], us.[ID]
但这一直只给我两个 tables 中的值。
我做错了什么?
您似乎想要在 full join
:
select
coalesce(de.id, us.id) as id,
case when de.id is null then 0 else 1 end as intablede,
max(de.country) as country,
coalesce(max(de.key1), 0) as dekey1,
coalesce(max(de.key2), 0) as dekey2,
case when us.id is null then 0 else 1 end as intableus,
coalesce(max(us.key1), 0) as uskey1,
coalesce(max(us.key2), 0) as uskey2
from dbo.tablede de
full join dbo.tableus us on de.id = us.id
group by de.id, us.id
order by id
id | intablede | country | dekey1 | dekey2 | intableus | uskey1 | uskey2 -: | --------: | :------ | -----: | -----: | --------: | -----: | -----: 1 | 1 | US | 1 | 0 | 1 | 0 | 0 2 | 1 | US | 0 | 0 | 1 | 0 | 1 3 | 1 | US | 1 | 1 | 0 | 0 | 0 4 | 1 | DE | 1 | 1 | 1 | 1 | 1 5 | 1 | DE | 0 | 0 | 0 | 0 | 0 8 | 0 | null | 0 | 0 | 1 | 0 | 1 9 | 0 | null | 0 | 0 | 1 | 1 | 0