我有一个SearchImpressions表,该表具有一个嵌套的类型列,其中包含在搜索过程中显示的Product_Id和Price数组。我还有一个表格,其中包含用户单击搜索结果之一时的详细信息。
问题:
给出一个产品ID,我想找出按天分组的总印象数和点击次数。
产品搜索结果表
EventDateTime查询结果(ProductId [],Price [])
产品点击次数表
EventDateTime产品ID
必需的输出
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;
谢谢
答案 0 :(得分:1)
看来需要为 clicks.productId 的计数汇总函数添加谓词,或使用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 */
..
答案 1 :(得分:1)
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;