简体   繁体   English

如何在不丢失右表列中的值的情况下使用左连接添加其他列?

[英]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.

相关问题 在SQL连接操作中,如何从左侧联接中获取行,以及如何从右侧表中获取两列的汇总 - In an SQL join operation, how to get the rows from the left join and only the aggregate of two columns from the right table 如何在通过左连接过滤时从右表中提取所有列 - How to pull all columns from right table while filtering via left join SQL - 左连接后丢失列 - SQL - Losing columns after a left join LEFT JOIN 右表中的缺失值 - LEFT JOIN missing values from the right table Rails、Postgres:如何在不对列进行硬编码的情况下创建数据透视表并将其左连接到另一个表 - Rails, Postgres: How to CREATE a pivot table and LEFT JOIN it to another table without hardcoding the columns postgresql:通过左连接添加两个表的列并在使用时 - postgresql:add columns by left join two table and using case when MYSQL如何比较来自相同列的值并连接表 - how to MYSQL compare values from same columns and join table 如何 UNION/LEFT JOIN/INNER JOIN 来自 INFORMATION_SCHEMA.COLUMNS 和 CROSS APPLY TABLE 的结果? - How to UNION/LEFT JOIN/INNER JOIN results from INFORMATION_SCHEMA.COLUMNS and CROSS APPLY TABLE? 查询左联接而没有B表中的所有右行 - Query left join without all the right rows from B table 我如何在左连接中设置 null 值与右连接表中的最大值 - how i can set null values in left join in presto with the max values from the right joined table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM