【问题标题】:Using rank to enumerate the session使用排名枚举会话
【发布时间】:2020-10-12 21:43:25
【问题描述】:

您好,谁能帮我解决这个问题。我想做的是对我的会话ID进行排名,但它似乎不起作用,因为它只返回“1”。 第三行应该是 2,第 5 行应该有 2 排名,第 6 行应该有 3 排名等等。我尝试枚举会话。非常感谢你

  WITH
    hits AS (
    SELECT
        TIMESTAMP_SECONDS(visitstarttime) AS timestamp,
        fullVisitorId,
        CONCAT(fullvisitorId, CAST(visitStartTime AS STRING)) AS sessionId,
        hits.page.pagePath,
        hits.isEntrance,
        hits.isInteraction,
        hits.type,
        hits.hitNumber,
        MIN(hits.hitNumber) OVER (PARTITION BY fullVisitorId, visitStartTime) AS first_hit,  
    FROM `dl-training-bigquery.ND_177736451.ga_sessions_20191202` AS ga_table,
      UNNEST(hits) AS hits
    WHERE hits.type = 'PAGE'
    )
    
    SELECT
      fullVisitorId,
      sessionId,
      pagePath,
      timestamp,
      MIN(timestamp) AS visit_time,
      RANK() OVER(PARTITION BY CONCAT(fullvisitorId, "-", CAST(timestamp AS STRING)) ORDER BY sessionId ASC) AS Rank
    FROM hits
    WHERE type = 'PAGE'
    GROUP BY 1,2,3,4
    ORDER BY sessionId

【问题讨论】:

    标签: sql datetime google-bigquery window-functions


    【解决方案1】:

    大概,你想要:

    RANK() OVER(
        PARTITION BY fullvisitorId, sessionId
        ORDER BY timestamp 
    ) AS Rank
    

    这会按时间戳升序排列具有相同访问者和会话的组内的点击 - 这就是我理解您的问题的方式。

    【讨论】:

    • 非常感谢!它的完美
    猜你喜欢
    • 1970-01-01
    • 2021-09-30
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    相关资源
    最近更新 更多