我正在使用其他来源的数据创建一个新表。这些表与来自 customer_ids 的响应有关。根据响应,响应有多个 customer_id 记录。如果打开或单击消息,则该响应有单独的记录。
当我用我的代码创建新表时,如果有人打开并单击消息,我会得到结果,而不是将它们组合成一行。
响应表
campaign_key | 客户密钥 | 响应代码 |
---|---|---|
42d28dc8 | 111 | 10205 |
42d28dc8 | 111 | 11205 |
42d28dc8 | 111 | 10204 |
下面的代码是我试过的
SELECT
DISTINCT(customer_key)
,(CASE WHEN response_code IN ('10204','11204') THEN 1 ELSE 0 END) AS OPENED
,(CASE WHEN response_code IN ('10205','11205') THEN 1 ELSE 0 END) AS CLICKED
FROM Response_table
WHERE campaign_key = '42d28dc8'
AND customer_ref_key = '111'
我想要的:
客户密钥 | 打开 | 点击 |
---|---|---|
111 | 1 | 1 |
我得到的:
客户密钥 | 打开 | 点击 |
---|---|---|
111 | 1 | 0 |
111 | 0 | 1 |
你快到了;你只需要聚合结果集:
select
customer_key,
max(case when response_code in (10204, 11204) then 1 else 0 end) as opened
max(case when response_code in (10205, 11205) then 1 else 0 end) as clicked
from response_table
where campaign_key = '42d28dc8'
and customer_ref_key = 111
group by customer_key
注意:大概
response_code
和customer_ref_key
是数字,所以他们应该与数字进行比较(不是字符串,如customer_ref_key = '111'
)。