我似乎无法通过此查询向我提供正确的结果。
select
datepart(day, i.timestamp) as [day],
i.campaignID, i.creativeID
, count(distinct i.sessionID) as [Impressions]
, count(case when e.eventID in (1,2,3) then 1 else 0 end) as Clicks
from impressions i
left join Events e
on i.sessionID = e.sessionID
group by
datepart(day, i.timestamp)
, i.campaignID, i.creative
order by [day]
我也尝试在then子句中用e.eventid替换1。我在结果中获得了太多的点击。事件每个sessionid有多个记录,因为许多类型的事件可以在同一个会话中发生(翻转,滚动等)我想要计算的事件将具有1,2或3的偶数,因为这些将是3您可以点击的可能链接。我想要的是通过获得点击次数和相关点击来计算点击率,如果没有点击我希望它反映为0而不是从结果中丢失,这就是我得到的。
任何帮助表示感谢,提前感谢您的宝贵时间!
答案 0 :(得分:3)
尝试将第二个count
替换为sum
,如下所示:
select
datepart(day, i.timestamp) as [day],
i.campaignID, i.creativeID
, count(distinct i.sessionID) as [Impressions]
, sum(case when e.eventID in (1,2,3) then 1 else 0 end) as Clicks
from impressions i
left join Events e
on i.sessionID = e.sessionID
group by
datepart(day, i.timestamp)
, i.campaignID, i.creative
order by [day]
或者,您可以保留count
并将eventID
过滤转移到where
子句:
select
datepart(day, i.timestamp) as [day],
i.campaignID, i.creativeID
, count(distinct i.sessionID) as [Impressions]
, count(e.eventID) as Clicks
from impressions i
left join Events e
on i.sessionID = e.sessionID
where e.eventID in (1,2,3)
group by
datepart(day, i.timestamp)
, i.campaignID, i.creative
order by [day]
但我怀疑你的on
子句中需要额外的过滤条件。但是,我不知道你的桌子的结构是肯定的。