BigQuery - 计算行数直到到达特定行

BigQuery - Count the number of rows until a specific row is reached

我是编码和 Whosebug 的新手,如果我的问题听起来很愚蠢,我深表歉意。

我在 BigQuery 中有 Google 个分析数据,我正试图从中汲取见解。

我想做的是查看每个用户及其浏览时的行为。

具体来说,我想计算第一个 'add to cart' 事件之前 'product view' 事件的数量。

在附加示例中,它按时间戳升序排序,返回的值将是第一个 'add to cart' 事件之前的 12 'product view' 个事件。

我如何获得每个用户的价值?使用的代码如下:

SELECT 
  PARSE_DATE('%Y%m%d', Date) AS Date,
  fullVisitorId as UserID,
  hits.eventInfo.eventCategory,
  hits.eventInfo.eventAction as eventaction,
FROM `table_name`, UNNEST (hits) AS hits
WHERE hits.eventInfo.eventCategory IS NOT NULL
  AND hits.eventInfo.eventCategory = 'Ecommerce'
  AND hits.eventInfo.eventAction IN ('Product View', 'Add to Cart')
ORDER BY hits.time ASC

提前致谢。

I want to count the number of 'product view' events before their FIRST 'add to cart' event.

您可以使用 window 函数计算此值:

SELECT userId, seqnum - 1
FROM (SELECT t.*, hits,
             ROW_NUMBER() OVER (PARTITION BY ut.userId ORDER BY h.time) as seqnum,
             ROW_NUMBER() OVER (PARTITION BY ut.userId, eventAction ORDER BY h.time) as seqnum_ea
      FROM `table_name` t CROSS JOIN
           UNNEST(t.hits) AS hits
      WHERE hits.eventInfo.eventCategory IS NOT NULL AND
            hits.eventInfo.eventCategory = 'Ecommerce' AND
            hits.eventInfo.eventAction IN ('Product View', 'Add to Cart')
     ) t
WHERE hits.eventInfo.eventAction = 'Add to Cart' AND seqnum_ea = 1;