sql - 使用 Query 根据特定事件值对表进行透视

标签 sql pivot amazon-redshift

我想让表 A 和表 B 一样。 我想看看用户在购买事件之前引发了什么事件。 我已经使用 row_number() over(按 client_id 分区,按时间排序 event_type),它只是一个枢轴。我如何制定逻辑?

表A

client_id   event_type  count      time 
    A         cart        1     AM 12:00:00 
    A         view        4     AM 12:01:00
    A         purchase    2     AM 12:05:00
    A         view        2     AM 12:10:00 
    B         view        3     AM 12:03:00
    B         purchase    1     AM 12:05:00
    B         view        2     AM 12:10:00 

表B

client_id     view     cart   purchase 
    A           4        1        2     
    A           2        0        0
    B           3        0        1
    B           2        0        0

最佳答案

这是一种方法,我在购买前使用 block grp_split 将一组事件定义为属于单个“ session /事件”。

然后我通过使用 max(grp) over(partition by client_id order by time1) as grp2 将空值替换为先前非空值,从而在 block x 中正确完成此分组。

之后是旋转查看、购物车和购买的列

with data
  as (
    select 'A' as client_id,'cart'     as event_type   , 1  as count1, cast('AM 12:00:00' as time) as time1 union all 
    select 'A' as client_id,'view'     as event_type   , 4  as count1, cast('AM 12:01:00' as time) as time1 union all
    select 'A' as client_id,'purchase' as event_type   , 2  as count1, cast('AM 12:05:00' as time) as time1 union all
    select 'A' as client_id,'view'     as event_type   , 2  as count1, cast('AM 12:10:00' as time) as time1 union all
    select 'B' as client_id,'view'     as event_type   , 3  as count1, cast('AM 12:03:00' as time) as time1 union all
    select 'B' as client_id,'purchase' as event_type   , 1  as count1, cast('AM 12:05:00' as time) as time1 union all
    select 'B' as client_id,'view' as event_type   , 2  as count1, cast('AM 12:10:00' as time) as time1 
     )
   ,grp_split
   as(
select case when lag(event_type) over(partition by client_id order by time1)='purchase' 
              or lag(event_type) over(partition by client_id order by time1) is null 
             then
                 row_number() over(partition by client_id order by time1)
        end as grp
      ,*
  from data
      )
 select x.client_id
       ,max(case when event_type='view' then count1 else 0 end) as view
       ,max(case when event_type='cart' then count1 else 0 end) as cart
       ,max(case when event_type='purchase' then count1 else 0 end) as purchase
  from (
  select *
        ,max(grp) over(partition by client_id order by time1) as grp2
    from grp_split
       )x
  group by client_id
           ,grp2 
  order by client_id

输出

+-----------+------+------+----------+
| client_id | view | cart | purchase |
+-----------+------+------+----------+
| A         |    4 |    1 |        2 |
| A         |    2 |    0 |        0 |
| B         |    3 |    0 |        1 |
| B         |    2 |    0 |        0 |
+-----------+------+------+----------+

工作示例

https://dbfiddle.uk/?rdbms=postgres_12&fiddle=aeeb0878b9094e061c469bb0efb7a024

关于sql - 使用 Query 根据特定事件值对表进行透视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63261104/

相关文章:

c# - F#:连接到 Azure SQL 数据库?

mysql - 如何为 Amazon Redshift 外部表建模以更改列结构?

java - Redshift - 选择查询表名称在双引号中不起作用

mysql - 用于中型数据的 BigQuery 替代方案

sql - 尝试将客户添加到数据库时出错(插入语句oledb vb net中的语法错误)

sql - 函数 SQL 中的函数

php - 如何在jquery中获取php echo数据

sql - 为此使用动态数据透视查询?

postgresql - 数据透视表/交叉表 : I wan to return dynamic columns

具有 3 个表的 MySQL 数据透视表