如何使用左联接添加其他列而又不丢失右表中列的值?

时间:2020-07-03 07:10:16

标签: sql google-bigquery

表A 国家(地区)|日期来源|广告活动|潜在客户(总和)

表B 国家(地区)|日期来源|广告活动|购买(总和)|收入(总和)

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

所需的输出:

国家|日期来源|广告活动|潜在客户|购买|收入

执行此操作时,潜在客户总数与表A中的总数相同,但是加入后表B的购买总数和收入总额下降。情况并非如此。

我无法弄清楚为什么会是这种情况-想法是并非所有广告系列线索都导致购买,因此是“左联接”而不是“内联接”。

更新

表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

表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   

Output data with outer or full joins

3 个答案:

答案 0 :(得分:0)

似乎希望得到潜在客户,购买和收入的总和。请使用以下查询,

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;

如果这不是您所期望的,请提供示例数据和您期望的结果。为您提供解决方案会更有帮助

答案 1 :(得分:0)

我认为,如果需要从两个表中获取所有数据,则需要使用完全外部联接:

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

如果我误解了您的问题,请提供示例数据

更新:

尝试一下:

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

答案 2 :(得分:0)

假设您的join条件不会产生重复,我建议将full joinusing结合使用:

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);

这将保留两个表中的所有行。使用USING子句可以轻松避免在NULL中使用的键列上使用JOIN值。 COALESCE()是因为我猜测您确实只希望为一个表中的行提供0值-但如果对NULL感到满意,可以将其删除。