Clickhouse Array Join with Left Join 和计算计数

Clickhouse Array Join with Left Join and Computed Counts

我有一个 SearchImpressions table,它有一个嵌套类型列,其中包含 Product_Id 数组和搜索期间显示的价格。我还有一个 table 保存有关用户何时单击其中一个搜索结果的详细信息。

问题:

给定一个产品 ID,我想找出按天分组的总展示次数和点击次数。

产品搜索结果Table

EventDateTime 查询结果(ProductId[], Price[])

产品点击次数Table

EventDateTime ProductId

需要输出

EventDate    ProductId TotalImpressions TotalClicks

11-11-2020   0001      56               6
12-11-2020   0002      21               0

我试过了,但两个计数看起来一样

SELECT pr.EventDate,
       impressions.ProductId,
       count(impressions.ProductId) As TotalImpressions,
       count(clicks.productId) as TotalClicks
FROM ProductResults pr
ARRAY JOIN results as impressions
LEFT JOIN ProductClicks clicks on
          impressions.ProductId = clicks.ProductId
GROUP BY pr.EventDate,
         pr.DealershipId,
         pr.Vrm
ORDER BY pr.EventDate Desc;

谢谢

看起来需要为 clicks.productId 添加谓词到 count-aggregate 函数或使用 uniqIf-function:

SELECT pr.EventDate,
       impressions.ProductId,
       count(impressions.ProductId) As TotalImpressions,
       countIf(clicks.productId != 0) as TotalClicks1 /* <-- v.1 */
       uniqIf(clicks.productId, clicks.productId != 0) as TotalClicks2 /* <-- v.2 */
..
 SELECT pr.EventDate,
        impressions.ProductId,
        count() As TotalImpressions,
        clicks.TotalClicks
 FROM ProductResults pr ARRAY JOIN results as impressions
 LEFT JOIN    (select ProductId, count(clicks.productId) TotalClicks 
               from  ProductClicks group by ProductId 
              ) clicks on impressions.ProductId = clicks.ProductId
 GROUP BY pr.EventDate, impressions.ProductId
 ORDER BY pr.EventDate Desc;