[英]How to add additional columns with a Left Join without losing values in columns from right table?
Table A Country |表 A国家 | Date |日期 | Source |来源 | Campaign |活动 | Leads (sum)潜在客户(总和)
Table B Country |表 B国家 | Date |日期 | Source |来源 | Campaign |活动 | Purchases (sum) |采购(总和)| Revenue (sum)收入(总和)
SELECT
a.*,
b.purchases,
b.revenue
FROM
Table A
LEFT JOIN
Table B
ON a.country = b.country and a.date = b.date and a.source = b.source and a.campaign = b.campaign
Desired Output:所需的 Output:
Country |国家 | Date |日期 | Source |来源 | Campaign |活动 | Leads |线索 | Purchases |采购 | Revenue收入
When I do this, the total number of leads remain the same as in Table A but the total number of purchases and sum of revenue from Table B drops after the join.当我这样做时,潜在客户总数与表 A 中的相同,但表 B 的购买总数和收入总和在加入后下降。 This should not be the case.这不应该是这样。
I'm not able to figure out why this would be the case - the idea is that not all campaign leads result in a purchase, hence the Left Join instead of Inner Join.我无法弄清楚为什么会出现这种情况 - 想法是并非所有活动线索都会导致购买,因此是左连接而不是内连接。
Update:更新:
Table A表 A
country times sources camp leads
Germany 01-Jan-20 Facebook a 227
Germany 02-Jan-20 Facebook b 175
Germany 03-Jan-20 Facebook c 215
Germany 04-Jan-20 Facebook 23
Germany 05-Jan-20 Facebook 251
Germany 06-Jan-20 Facebook d 135
Germany 07-Jan-20 Facebook 257
Germany 08-Jan-20 Facebook e 54
Germany 09-Jan-20 Facebook 51
Germany 10-Jan-20 Facebook 27
Germany 11-Jan-20 Facebook 264
Germany 12-Jan-20 Facebook f 41
Germany 13-Jan-20 Facebook 359
Germany 14-Jan-20 Facebook 2
Germany 15-Jan-20 Facebook g 33
Table B表 B
country times sources camp purchases revenue
Germany 01-Jan-20 Facebook a
Germany 02-Jan-20 Facebook b
Germany 03-Jan-20 Facebook c 1 127.88
Germany 04-Jan-20 Facebook 1 93.42
Germany 05-Jan-20 Facebook 1 74.18
Germany 06-Jan-20 Facebook d
Germany 07-Jan-20 Facebook
Germany 08-Jan-20 Facebook e
Germany 09-Jan-20 Facebook
Germany 10-Jan-20 Facebook 1 85.36
Germany 11-Jan-20 Facebook 1 79.77
Germany 12-Jan-20 Facebook f 1 121.01
Germany 13-Jan-20 Facebook
Germany 14-Jan-20 Facebook
Germany 15-Jan-20 Facebook g 1
Seems to be your expectation is to get the sum of Leads, Purchases and Revenue.似乎您的期望是获得线索、购买和收入的总和。 Please use below query,请使用以下查询,
SELECT
a.Country, a.Date, a.Source, a.Campaign, sum(a.Leads) sum(b.purchases), sum(b.revenue)
FROM
Table A
LEFT JOIN
Table B
ON a.country = b.country and a.date = b.date and a.source = b.source and a.campaign =
b.campaign;
If this is not what you are expecting, please provide sample data and your expected result.如果这不是您所期望的,请提供示例数据和您的预期结果。 Would be more helpful to provide you the solution为您提供解决方案会更有帮助
I think you need to use full outer join if you need to get all the data from both tables:如果您需要从两个表中获取所有数据,我认为您需要使用完全外连接:
SELECT
ifnull(a.country,b.country) country,
ifnull(a.date,b.date) date,
ifnull(a.source,b.source) source,
ifnull(a.campaign,b.campaign) campaign,
a.leads,
b.purchases,
b.revenue
FROM
Table A
FULL OUTER JOIN
Table B
ON a.country = b.country and a.date = b.date and a.source = b.source and a.campaign = b.campaign
if I misunderstood you question, please provide a sample data如果我误解了您的问题,请提供示例数据
update:更新:
try this:尝试这个:
WITH
table_a AS (
SELECT
'A' Country,
'2020-01-01' Date,
'X' Source,
'L' Campaign,
100 Leads
UNION ALL
SELECT
'A' Country,
'2020-01-01' Date,
'X' Source,
'L' Campaign,
200 Leads
UNION ALL
SELECT
'B' Country,
'2020-01-01' Date,
'Y' Source,
'M' Campaign,
300 Leads
UNION ALL
SELECT
'B' Country,
'2020-01-01' Date,
'Y' Source,
'M' Campaign,
400 Leads ),
table_b AS (
SELECT
'A' Country,
'2020-01-01' Date,
'X' Source,
'L' Campaign,
100 Purchases,
100000 revenue
UNION ALL
SELECT
'A' Country,
'2020-01-01' Date,
'X' Source,
'L' Campaign,
200 Purchases,
30000
UNION ALL
SELECT
'B' Country,
'2020-01-01' Date,
'Y' Source,
'M' Campaign,
400 Purchases,
40000
UNION ALL
SELECT
'C' Country,
'2020-01-01' Date,
'X' Source,
'L' Campaign,
200 Purchases,
30000)
SELECT
ifnull(a.country,
b.country) country,
ifnull(a.date,
b.date) date,
ifnull(a.source,
b.source) source,
ifnull(a.campaign,
b.campaign) campaign,
a.leads,
b.purchases,
b.revenue
FROM
table_a a
FULL OUTER JOIN
table_b b
ON
a.country = b.country
AND a.date = b.date
AND a.source = b.source
AND a.campaign = b.campaign
Assuming that your join
conditions do not product duplicates, I would suggest using full join
with using
:假设您的join
条件不产生重复,我建议使用full join
using
:
SELECT country, date, source, campaign,
COALESCE(a.leads, 0) as leads,
COALESCE(b.purchases, 0) as purchases,
COALESCE(b.revenue, 0) as revenue
FROM TableA a FULL JOIN
TableB b
USING (country, date, source, campaign);
This will keep all rows in both tables.这将保留两个表中的所有行。 The USING
clause makes it easy to avoid NULL
values on the key columns used in the JOIN
. USING
子句可以轻松避免JOIN
中使用的键列上的NULL
值。 The COALESCE()
is because I'm guessing you really want 0
values for the rows that are in only one table -- but you can remove it if you are happy with NULL
. COALESCE()
是因为我猜你真的想要只有一个表中的行的0
值——但如果你对NULL
感到满意,你可以删除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.