Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
分组顺序重复值sqlite_Sql_Database_Sqlite_Window Functions_Gaps And Islands - Fatal编程技术网

分组顺序重复值sqlite

分组顺序重复值sqlite,sql,database,sqlite,window-functions,gaps-and-islands,Sql,Database,Sqlite,Window Functions,Gaps And Islands,我有按顺序重复的数据 A A A B B B A A A 我需要像这样把他们分组 A B A 使用sqlite执行此操作的最佳方法是什么?假设您有一个定义行顺序的列,例如id,您可以通过窗口函数解决此间隙和孤岛问题: select col, count(*) cnt, min(id) first_id, max(id) last_id from ( select t.*, row_number() over(order by id) rn1, row_

我有按顺序重复的数据

A
A
A
B
B
B
A
A
A
我需要像这样把他们分组

A
B
A

使用sqlite执行此操作的最佳方法是什么?

假设您有一个定义行顺序的列,例如
id
,您可以通过窗口函数解决此间隙和孤岛问题:

select col, count(*) cnt, min(id) first_id, max(id) last_id
from (
    select t.*,
        row_number() over(order by id) rn1,
        row_number() over(partition by col order by id) rn2
    from mytable t
) t
group by col, rn1 - rn2
order by min(id)

我在结果集中添加了一些列,这些列提供了有关每个组内容的更多信息。

如果您定义了一个列来定义行的顺序,例如
id
,则可以使用窗口函数
LEAD()

select col
from (
  select col, lead(col, 1, '') over (order by id) next_col
  from tablename
)
where col <> next_col
| col |
| --- |
| A   |
| B   |
| A   |