sql—如何添加具有左联接的其他列,而不丢失右表中列的值?

m4pnthwp  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(447)

表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

z9gpfhce

z9gpfhce1#

似乎你的期望是得到的线索,购买和收入的总和。请使用下面的查询,

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;

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

vcudknz3

vcudknz32#

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

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
zphenhs4

zphenhs43#

假设你的 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 .

相关问题