我有一个名为mixpanel的表,其中包含以下列
merchant_name
date
event_type (2 types 'Loaded a page', 'Clicked')
我还有一个名为google_webmaster的表,其中包含以下列
merchant_name
gw_clicks
gw_impressions
device (Can be PC, Mobile or Tablet)
date
我要做的是根据merchant_name加入这两个表并尝试获取商家名称,总和(加载页面),总和(点击次数),gw_impressions,gw_clicks
这是我当前的查询
SELECT
a.merchant_name,
b.gw_impressions,
b.gw_clicks,
SUM(CASE WHEN event_type = 'Loaded a Page' then 1 else 0 end) mxp_impressions,
SUM(CASE WHEN event_type = 'Clicked' then 1 else 0 end) mxp_clicks
from mixpanel a
inner join google_webmaster b on a.merchant_name = b.merchant_name
where a.time >= '2015-07-15 00:00:00' AND a.time <= '2015-07-15 23:59:59' and b.date = '2015-07-15'
group by 1,2,3
此查询的问题在于它将每个商家记录的3行从网站管理员表连接到mixpanel表。这是因为网站管理员表中的每台PC都有3行,Mobile,Tablet。
如何在加入gw_impressions的联接(对于每个商家)中获得1条记录。 gw_clicks适用于PC,手机和平板电脑。
答案 0 :(得分:0)
SELECT
a.merchant_name,
b.device,
SUM(b.gw_impressions + b.gw_clicks),
SUM(CASE WHEN event_type = 'Loaded a Page' then 1 else 0 end) mxp_impressions,
SUM(CASE WHEN event_type = 'Clicked' then 1 else 0 end) mxp_clicks
from mixpanel a
inner join google_webmaster b on a.merchant_name = b.merchant_name
where a.time >= '2015-07-15 00:00:00'
AND a.time <= '2015-07-15 23:59:59'
and b.date = '2015-07-15'
group by 1,2
在查询中添加了device
并总结了gw_impressions,gw_clicks。