Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.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
Sql 统计表中按顺序相同的行数_Sql_Sql Server_Gaps And Islands - Fatal编程技术网

Sql 统计表中按顺序相同的行数

Sql 统计表中按顺序相同的行数,sql,sql-server,gaps-and-islands,Sql,Sql Server,Gaps And Islands,我有一张像这样的桌子 +----+------------+------+ | ID | Session_ID | Type | +----+------------+------+ | 1 | 1 | 2 | | 2 | 1 | 4 | | 3 | 1 | 2 | | 4 | 2 | 2 | | 5 | 2 | 2 | | 6 |

我有一张像这样的桌子

 +----+------------+------+
 | ID | Session_ID | Type |
 +----+------------+------+
 |  1 |          1 |    2 |
 |  2 |          1 |    4 |
 |  3 |          1 |    2 |
 |  4 |          2 |    2 |
 |  5 |          2 |    2 |
 |  6 |          3 |    2 |
 |  7 |          3 |    1 |
 +----+------------+------+
我想统计一下序列中出现的
类型的所有事件

输出看起来是这样的:

 +------------+------+-----+
 | Session_ID | Type | cnt |
 +------------+------+-----+
 |          1 |    2 |   1 |
 |          1 |    4 |   1 |
 |          1 |    2 |   1 |
 |          2 |    2 |   2 |
 |          3 |    2 |   1 |
 |          3 |    1 |   1 |
 +------------+------+-----+
一个简单的
分组方法

SELECT session_id, type, COUNT(type)
FROM table
GROUP BY session_id, type
不起作用,因为我只需要对“接触”的行进行分组

这是可能的,仅仅是一个sql选择还是我需要某种编码。存储过程还是应用程序端编码

更新顺序:
如果下一行具有相同的类型,则应对其进行计数(按Id排序)

要确定序列,
ID
是带有
session\u ID
的键,因为我只想在同一个session中对类型进行分组

所以,如果有3行正在进行会话

  • ID为1的行的类型为1
  • 第二行的类型是1
  • 第3行有类型2
输入:

 +----+------------+------+
 | ID | Session_ID | Type |
 +----+------------+------+
 |  1 |          1 |    1 |
 |  2 |          1 |    1 |
 |  3 |          1 |    2 |
 +----+------------+------+
 +------------+------+-----+
 | Session_ID | Type | cnt |
 +------------+------+-----+
 |          1 |    1 |   2 |
 |          3 |    2 |   1 |
 +------------+------+-----+
第1行到第2行的顺序。这三行应该输出

输出:

 +----+------------+------+
 | ID | Session_ID | Type |
 +----+------------+------+
 |  1 |          1 |    1 |
 |  2 |          1 |    1 |
 |  3 |          1 |    2 |
 +----+------------+------+
 +------------+------+-----+
 | Session_ID | Type | cnt |
 +------------+------+-----+
 |          1 |    1 |   2 |
 |          3 |    2 |   1 |
 +------------+------+-----+

您可以使用
id
row\u number()
的差值来识别间隙,然后进行计数

;with cte as
(
Select *, id - row_number() over (partition by session_id,type order by id) as grp 
from table
)
select session_id,type,count(*) as cnt
from cte
group by session_id,type,grp 
order by max(id) 

您可以使用
id
row\u number()
的差值来识别间隙,然后进行计数

;with cte as
(
Select *, id - row_number() over (partition by session_id,type order by id) as grp 
from table
)
select session_id,type,count(*) as cnt
from cte
group by session_id,type,grp 
order by max(id) 

定义
序列
。序列中有什么?另外,您使用的是什么
dbms
<代码>SQL
只是一种语言。序列如示例表1,2,3,。。。按ID排序。(将更新问题,使其更清楚)。MSSQL。我会更新标签,但顺序是什么?示例表中有
2,2,2
,但相应的计数是
1,2,1
????您提到了
类型
,但您的评论提到了
ID
???定义
序列
。序列中有什么?另外,您使用的是什么
dbms
<代码>SQL
只是一种语言。序列如示例表1,2,3,。。。按ID排序。(将更新问题,使其更清楚)。MSSQL。我会更新标签,但顺序是什么?示例表中有
2,2,2
,但相应的计数是
1,2,1
????你提到了
类型
,但你的评论提到了
ID
??@winner\u joiner。这个答案假设
id
是连续的,没有间隔。在数据库中通常不能保证这一点。@winner\u joiner。这个答案假设
id
是连续的,没有间隔。在数据库中通常无法保证这一点。