我正在寻找一种方法,通过sql,建立一个 visitor_id
来自共享公共(但不同)键的行,然后生成一个输出表,分配已建立的 visitor_id
回到所有的行。我的数据库是aws雅典娜。
例子:
with things as (
select 'aaa' as thing_id, '111' as visitor_id, 'alpha@gmail.com' as email_address, '(111) 111-1111' as phone_number
union all select 'bbb' as thing_id, '111' as visitor_id, 'beta@gmail.com' as email_address, null as phone_number
union all select 'ccc' as thing_id, '111' as visitor_id, null as email_address, '(222) 222-2222' as phone_number
union all select 'ddd' as thing_id, '222' as visitor_id, 'alpha@gmail.com' as email_address, '(333) 333-3333' as phone_number
union all select 'eee' as thing_id, '333' as visitor_id, 'alpha@gmail.com' as email_address, null as phone_number
union all select 'fff' as thing_id, '444' as visitor_id, 'charlie@gmail.com' as email_address, '(333) 333-3333' as phone_number
union all select 'ggg' as thing_id, '555' as visitor_id, 'charlie@gmail.com' as email_address, '(444) 444-4444' as phone_number
union all select 'hhh' as thing_id, '666' as visitor_id, null as email_address, '(444) 444-4444' as phone_number
)
我知道所有这些东西都属于同一个世界 visitor_id
作为 aaa
, bbb
,和 ccc
都是一样的
visitor_id ddd
以及 eee
共享相同的 email_address
作为 aaa
, bbb
,和
ccc fff
共享相同的 phone_number
作为 ddd
(因此也是如此 vistior_id
作为 aaa
) ggg
共享相同的 email_address
作为 fff
(因此也是如此 vistior_id
作为 fff
哪个是一样的 visitor_id
作为 aaa
) hhh
共享相同的 phone_number
作为 ggg
(因此也是如此 vistior_id
作为 ggg
这和 aaa
)
我想要的输出表是:
|---------------------|------------------|
| thing_id | vistior_id |
|---------------------|------------------|
| aaa | 111 |
|---------------------|------------------|
| bbb | 111 |
|---------------------|------------------|
| ccc | 111 |
|---------------------|------------------|
| ddd | 111 |
|---------------------|------------------|
| eee | 111 |
|---------------------|------------------|
| fff | 111 |
|---------------------|------------------|
| ggg | 111 |
|---------------------|------------------|
| hhh | 111 |
|---------------------|------------------|
我已经尝试交叉连接,然后消除重复。然而,我正在与 fff
, ggg
,和 hhh
,因为它们只与 aaa
通过另一个 thing_id
s
暂无答案!
目前还没有任何答案,快来回答吧!