sql以获取一些组并保持顺序

bxgwgixi  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(277)

我有一个例子数据如下。

+---------+------------+--------+
| user id |   sequence | Action |
|---------|------------|--------|
| 12345   |    1       | Run    |
| 12345   |    2       | Sit    |
| 12345   |    3       | Sit    |
| 12345   |    4       | Run    |
| 12345   |    5       | Run    |
| 12345   |    6       | Sit    |
+---------+------------+--------+

现在我想结果应该是这样的:

+---------+---------+
| user id |  Action |
|---------|---------|
| 12345   |  Run    |
| 12345   |  Sit    |
| 12345   |  Run    |
| 12345   |  Sit    |
+---------+---------+

顺序为#2和#3的行应合并,#4和#5应合并。我使用“按操作分组”将得到下表所示的答案,但这不是我想要的:

+---------+---------+
| user id |  Action |
|---------|---------|
| 12345   |  Run    |
| 12345   |  Sit    |
+---------+---------+

如何实现sql(我使用googlebigquery)?
万分感谢!

v8wbuo2f

v8wbuo2f1#

下面是bigquery标准sql


# standardSQL

SELECT * EXCEPT(dup) FROM (
  SELECT *, action = LAG(action, 1, '') OVER(PARTITION BY user_id ORDER BY sequence) AS dup
  FROM `project.dataset.table`
)
WHERE NOT dup

如果要应用到问题输出的样本数据

Row user_id sequence    action   
1   12345   1           Run  
2   12345   2           Sit  
3   12345   4           Run  
4   12345   6           Sit
pgvzfuti

pgvzfuti2#

您可以使用窗口函数:其思想是将每行上的操作与“上一个”操作进行比较,并对值更改的行进行筛选:

select *
from (
    select t.*, lag(action) over(partition by user_id order by sequence) lag_action
    from mytable t
) t
where action <> lag_action or lag_action is null

相关问题