【问题标题】:BigQuery - Custom Lag Offset while using Lag functionBigQuery - 使用滞后功能时的自定义滞后偏移量
【发布时间】:2015-10-28 05:58:09
【问题描述】:

我有一个如下 BigQuery 表:

date  hits_eventInfo_Category  hits_eventInfo_Action  session_id  user_id  hits_time  hits_eventInfo_Label

20151021  Air  Search  1445001  A232  1952  City1
20151021  Air  Select  1445001  A232  2300  Vendor1
20151021  Air  Search  1445001  A111  1000  City2
20151021  Air  Search  1445001  A111  1900  City3
20151021  Air  Select  1445001  A111  7380  Vendor2
20151021  Air  Search  1445001  A580  1000  City4
20151021  Air  Search  1445001  A580  1900  City5
20151021  Air  Search  1445001  A580  1900  City6
20151021  Air  Select  1445001  A580  7380  Vendor3

该表显示了 3 个用户 - A232、A111 和 A580 的用户活动,因此:

i) A232 - Made 1 Search at 'City1' and chose 'Vendor1' from 'City1'
ii) A111 - Made the 1st search at 'City2' and did not choose any vendor from there. Made a 2nd search at 'City3' and then ultimately chose a 'Vendor2' from here.
iii) A580 - 1st search at 'City4', no vendor chosen. 2nd search at 'City5', no vendor chosen. 3rd search at 'City6', 'Vendor3' chosen from City6.

我只对检索用户实际选择供应商的城市感兴趣,也就是说,对用户之前没有导致选择供应商的搜索不感兴趣。

所需的输出表:

date  hits_eventInfo_Category  hits_eventInfo_Action  session_id  user_id  hits_time  city  vendor

20151021  Air  Search  1445001  A232  1952  City1  Vendor1
20151021  Air  Search  1445001  A111  1900  City3  Vendor2
20151021  Air  Search  1445001  A580  1900  City6  Vendor3

在对 user_id 进行分区并按 hits_time 排序后,我一直尝试在 hits_eventInfo_eventLabel 字段上使用 LAG 函数,即LAG(hits_eventInfo_eventLabel,1) OVER( PARTITION BY user_id ORDER BY hits_time)

但是,由于我使用滞后偏移量为 1,因此上述表达式仅帮助我获得用户 A232 所需的输出(因为他只进行了 1 次搜索,这意味着在选择供应商之前的上一条记录肯定是搜索记录)。

有没有一种方法可以让这个滞后表达式更加动态,以便它只检索在进行选择之前搜索到的直接位置 - 而不管在进行选择之前进行了多少次搜索?

我可以采取其他功能/路线来实现这一目标吗?

【问题讨论】:

    标签: google-bigquery partitioning lag


    【解决方案1】:
    select 
      date, 
      hits_eventInfo_Category, 
      hits_eventInfo_Action, 
      session_id, 
      user_id, 
      hits_time, 
      prev as city, 
      hits_eventInfo_Label as vendor
    from (
      select *, 
        lag(hits_eventInfo_Label, 1) over(partition by user_id order by hits_time) as prev
      from dataset.table
    )
    where hits_eventInfo_Action = 'Select'
    

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多