日期:2014-05-16  浏览次数:20457 次

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);

可根据自己的情况自由发挥