1

我似乎无法得到这个查询来给我正确的结果。

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 子句中将 1 替换为 e.eventid 。结果中的点击次数太多了。事件每个 sessionid 有多个记录,因为许多类型的事件可以在同一个会话中发生(翻转、转出等)。我要计算的事件的 eventid 将是 1、2 或 3,因为它们将是 3您可以点击的可能链接。我正在寻找的是通过获取展示次数和相关点击次数来计算点击率,如果没有点击,我希望它反映为 0 而不是从结果中丢失,这就是我得到的。

任何帮助表示赞赏,提前感谢您的宝贵时间!

4

1 回答 1

3

尝试用 替换第二个countsum如下所示:

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并将其移动到子句:eventIDwhere

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子句中需要额外的过滤条件。但是,我不确定您的表格结构。

于 2013-09-30T19:25:00.987 回答