0

我需要帮助重写 Big Quer 中的 postgres 横向函数。情况就是这样。在一个会话期间,客户会访问多个网站页面。

  1. 我需要创建landing_page 列。Landing_page 是页面,其中 Page_view_order = 1。
  2. 我需要计算每个landing_page 的转化次数和唯一会话次数。

这是在 Postgres 中使用横向连接编写的表和查询。我需要重新编写它,以便它在 Big Query 中工作。

session_id | page | page_view_order | landing_page | conversion

         1 | A    |               1 | A            |           
         1 | B    |               2 | A            |          1
         1 | C    |               3 | A            |           
         2 | B    |               1 | B            |          4
         2 | C    |               2 | B            |           
         2 | F    |               3 | B            |           
         3 | D    |               1 | D            |           
         4 | K    |               1 | K            |          7

如果我们将上表命名为 T,那么。

    with T as (
    select 1 as session_id, 'A' as page, 1 as page_view_order, 'A' as landing_page, null as conversion union all
    select 1, 'B', 2, 'A', 1       union all
    select 1, 'C', 3, 'A', null    union all
    select 2, 'B', 1, 'B', 4       union all
    select 2, 'C', 2, 'B', null    union all
    select 2, 'F', 3, 'B', null    union all
    select 3, 'D', 1, 'D', null    union all
    select 4, 'K', 1, 'K', 7
)
select t1.page
     , t2.q1 as converion_cnt
     , nullif(t2.q2, 0) as uniq_session
  from (select distinct page from T) t1
  join lateral (
    select sum(conversion) as q1
         , count(distinct session_id) as q2
      from T
     where landing_page = t1.page
  ) t2 on true
order by 1

结果应该如下。

langing_page A:1 次转化和 1 次会话。langing_page B:4 次转化和 1 次会话。登陆页面 K:7 次转化和 1 次会话。如果会话期间没有转化,其他landing_pages 应该为转化返回空值。

4

0 回答 0