SQL- join不覆盖时如何添加直接对应ID的命名列
SQL- How to add in named columns directly corresponding to an ID when join doesn't cover them
我正在尝试根据日期、ID 和国家加入两个 table。 Table1多了一个直接对应ID列的列
table 1
| Date | ID |Country|ID name |Clicks
| ----------| --------|-------|------------------------|------- |
| 25/02/2021| 42587750|Spain |Targeting Details Spain |5
| 26/02/2021| 42587750|Spain |Targeting Details Spain |15
然而,另一个 table 跟踪国家的方式略有不同,所以它看起来像这样
Table 2
| Date | ID |Country|Clicks
| ----------| --------|-------|------|
| 25/02/2021| 42587750|Spain |4
| 25/02/2021| 42587750|France |1
| 26/02/2021| 42587750|Spain |13
| 26/02/2021| 42587750|Italy |2
我想查看国家的所有案例,所以我目前已经完成了基于日期、ID 和国家的完整连接。但是,这会产生这样的数据
Date
table1.ID
table1.Country
table2.ID
table2.Country
ID name
table1.Clicks
table2.clicks
25/02/2021
42587750
Spain
42587750
Spain
Targeting Details Spain
5
4
26/02/2021
42587750
Spain
42587750
Spain
Targeting Details Spain
15
13
25/02/2021
42587750
France
1
25/02/2021
42587750
Italy
1
我用它来加入:
from table1 full outer join table2 on table1.date=table2.date and table1.ID=table2.ID and table1.country=table2.country
当我加入的所有 3 件事都匹配时,我有所有正确的详细信息,但是当国家/地区不同时,我就没有 ID 名称了。
是否可以在国家不匹配的情况下,在拉入国家的同时拉取ID名称?
像这样取出国家级别的信息就能得到正确的数字
from table1 full outer join table2 on table1.date=table2.date and table1.ID=table2.ID
我已经尝试使用 AVG 函数而不是 SUM 来计算点击次数,但没有奏效。
您的查询结构是否使用 coalesce()
?
select coalesce(t1.date, t2.date) as date,
coalesce(t1.id, t2.id) as id,
coalesce(t1.country, t2.country),
coalesce(t1.idname, max(t1.idname) over (partition by coalesce(t1.id, t2.id)) as idname,
t1.clicks, t2.clicks
from table1 t1 full outer join
table2 t2
on t1.date = t2.date and
t1.ID = t2.ID and
t1.country = t2.country
我正在尝试根据日期、ID 和国家加入两个 table。 Table1多了一个直接对应ID列的列 table 1
| Date | ID |Country|ID name |Clicks
| ----------| --------|-------|------------------------|------- |
| 25/02/2021| 42587750|Spain |Targeting Details Spain |5
| 26/02/2021| 42587750|Spain |Targeting Details Spain |15
然而,另一个 table 跟踪国家的方式略有不同,所以它看起来像这样
Table 2
| Date | ID |Country|Clicks
| ----------| --------|-------|------|
| 25/02/2021| 42587750|Spain |4
| 25/02/2021| 42587750|France |1
| 26/02/2021| 42587750|Spain |13
| 26/02/2021| 42587750|Italy |2
我想查看国家的所有案例,所以我目前已经完成了基于日期、ID 和国家的完整连接。但是,这会产生这样的数据
Date | table1.ID | table1.Country | table2.ID | table2.Country | ID name | table1.Clicks | table2.clicks |
---|---|---|---|---|---|---|---|
25/02/2021 | 42587750 | Spain | 42587750 | Spain | Targeting Details Spain | 5 | 4 |
26/02/2021 | 42587750 | Spain | 42587750 | Spain | Targeting Details Spain | 15 | 13 |
25/02/2021 | 42587750 | France | 1 | ||||
25/02/2021 | 42587750 | Italy | 1 |
我用它来加入:
from table1 full outer join table2 on table1.date=table2.date and table1.ID=table2.ID and table1.country=table2.country
当我加入的所有 3 件事都匹配时,我有所有正确的详细信息,但是当国家/地区不同时,我就没有 ID 名称了。
是否可以在国家不匹配的情况下,在拉入国家的同时拉取ID名称?
像这样取出国家级别的信息就能得到正确的数字
from table1 full outer join table2 on table1.date=table2.date and table1.ID=table2.ID
我已经尝试使用 AVG 函数而不是 SUM 来计算点击次数,但没有奏效。
您的查询结构是否使用 coalesce()
?
select coalesce(t1.date, t2.date) as date,
coalesce(t1.id, t2.id) as id,
coalesce(t1.country, t2.country),
coalesce(t1.idname, max(t1.idname) over (partition by coalesce(t1.id, t2.id)) as idname,
t1.clicks, t2.clicks
from table1 t1 full outer join
table2 t2
on t1.date = t2.date and
t1.ID = t2.ID and
t1.country = t2.country