欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

Oracle 按相同数据连续统计

程序员文章站 2022-06-17 15:29:46
...

Oracle 按相同数据连续统计 CREATE TABLE TEST(ID NUMBER(20),val NUMBER(20))INSERT INTO TEST VALUES(1,5);INSERT INTO TEST V

Oracle 按相同数据连续统计

CREATE TABLE TEST(
ID NUMBER(20),
val NUMBER(20)
)
INSERT INTO TEST VALUES(1,5);
INSERT INTO TEST VALUES(2,10);
INSERT INTO TEST VALUES(3,10);
INSERT INTO TEST VALUES(4,10);
INSERT INTO TEST VALUES(5,6);
INSERT INTO TEST VALUES(6,6);
INSERT INTO TEST VALUES(7,10);
INSERT INTO TEST VALUES(8,10);


SELECT val,COUNT(*) FROM
(
SELECT ID,val,
row_number() OVER(ORDER BY ID)-row_number() OVER(PARTITION BY val ORDER BY ID) x
FROM TEST
)
GROUP BY val,x

ORDER BY MIN(ID);

可根据自己的情况*发挥

Oracle 按相同数据连续统计