连接数据而不从第一个表创建重复的度量标准行,(第二个表包含更多行但不包含度量标准)

时间:2017-08-31 09:49:26

标签: postgresql

我有以下两个表格,我想加入一个全面的数字营销报告,而不会在指标方面产生重复。我们的想法是获取竞争对手的广告,并将其与我现有的营销数据结合起来,如下所示;

 Campaign|Impressions | Clicks | Conversions | CPC |Key                             
---------+------------+--------+-------------+-----+----
  USA-SIM|53432       |  5001  |            5| 2$  |Hgdy24
  DE-SIM |5389        |  4672  |            3| 4$  |dhfg12

竞争对手的数据如下;

     Key | Ad Copie   |
---------+------------+
  Hgdy24 |Click here! |
  Hgdy24 |Free Trial! |
  Hgdy24 |Sign Up now |
  dhfg12 |Check it out|
  dhfg12 |World known |
  dhfg12 |Sign up     |

使用传统的连接查询会产生以下不可用的结果

 Campaign|Impressions | Clicks | Conversions | CPC |Key   |Ad Copie                    
---------+------------+--------+-------------+-----+------+---------
  USA-SIM|53432       |  5001  |            5| 2$  |Hgdy24|Click here!
  USA-SIM|53432       |  5001  |            5| 2$  |Hgdy24|Free Trial!
  USA-SIM|53432       |  5001  |            5| 2$  |Hgdy24|Sign Up now 
  DE-SIM |5389        |  4672  |            3| 4$  |dhfg12|Check it out
  DE-SIM |5389        |  4672  |            3| 4$  |dhfg12|World known
  DE-SIM |5389        |  4672  |            3| 4$  |dhfg12|Sign up 

这是所需的输出

 Campaign|Impressions | Clicks | Conversions | CPC |Key   |Ad Copie                    
---------+------------+--------+-------------+-----+------+---------
  USA-SIM|53432       |  5001  |            5| 2$  |Hgdy24|Click here!
  USA-SIM|            |        |             |     |Hgdy24|Free Trial!
  USA-SIM|            |        |             |     |Hgdy24|Sign Up now 
  DE-SIM |5389        |  4672  |            3| 4$  |dhfg12|Check it out
  DE-SIM |            |        |             |     |dhfg12|World known
  DE-SIM |            |        |             |     |dhfg12|Sign up 

或作为替代方案也可以

 Campaign|Impressions | Clicks | Conversions | CPC |Key   |Ad Copie                    
---------+------------+--------+-------------+-----+------+---------
  USA-SIM|53432       |  5001  |            5| 2$  |Hgdy24|
  USA-SIM|            |        |             |     |Hgdy24|Click here!
  USA-SIM|            |        |             |     |Hgdy24|Free Trial!
  USA-SIM|            |        |             |     |Hgdy24|Sign Up now 
  DE-SIM |5389        |  4672  |            3| 4$  |dhfg12|
  DE-SIM |            |        |             |     |dhfg12|Check it out
  DE-SIM |            |        |             |     |dhfg12|World known
  DE-SIM |            |        |             |     |dhfg12|Sign up 

我还没有找到一个不会产生额外指标的工作。

最近的结果

 campaing | impressions | clicks | conversions | cpc |  key   |   ad_copie
----------+-------------+--------+-------------+-----+--------+------------
 USA-SIM  |       53432 |   5001 |           5 | 2$  |        |
 USA-SIM  |             |        |             |     | Hgdy24 | Click here!
 USA-SIM  |             |        |             |     | Hgdy24 | Free Trial!
 USA-SIM  |             |        |             |     | Hgdy24 | Sign Up now
 DE-SIM   |        5389 |   4672 |           3 | 4$  |        |
 DE-SIM   |             |        |             |     | dhfg12 | Check it out
 DE-SIM   |             |        |             |     | dhfg12 | World known
 DE-SIM   |             |        |             |     | dhfg12 | Sign up

1 个答案:

答案 0 :(得分:0)

您可以使用窗口函数lag()来检查前一行中key的内容,并显示指标或将其归零。

select campaing,
       case when prev_key is null or prev_key != key then impressions end as impressions,
       case when prev_key is null or prev_key != key then clicks end as clicks,
       case when prev_key is null or prev_key != key then conversions end as conversions,
       case when prev_key is null or prev_key != key then cpc end as cpc,
       key, ad_copie
  from (
    select campaing, lag(key) over () AS prev_key, impressions, clicks, conversions, cpc, key, ad_copie
    from ad1
    join comp1 using(key)
    order by campaing desc, key
  ) sub;

结果:

 campaing | impressions | clicks | conversions | cpc |  key   |   ad_copie
----------+-------------+--------+-------------+-----+--------+--------------
 USA-SIM  |       53432 |   5001 |           5 | 2$  | Hgdy24 | Click here!
 USA-SIM  |             |        |             |     | Hgdy24 | Free Trial!
 USA-SIM  |             |        |             |     | Hgdy24 | Sign Up now
 DE-SIM   |        5389 |   4672 |           3 | 4$  | dhfg12 | Check it out
 DE-SIM   |             |        |             |     | dhfg12 | World known
 DE-SIM   |             |        |             |     | dhfg12 | Sign up
(6 wierszy)

编辑:您可能需要在NULL指标之前修改您比较的列,并且可能需要修改您将订购数据的列。如果密钥对于竞选是唯一的,那么我认为这就足够了。