连续日期统计

连续日期统计

表结构:
create table TEST
(
  ID VARCHAR2(8),
  RQ DATE,
  JE NUMBER
)


表数据:

连续日期统计

查询语句:

SELECT B.ID, (MAX(B.rq) - MIN(B.rq)+1) DAYS,MIN(B.rq) ks, MAX(B.rq) js,sum(je)
          FROM (SELECT A.*, to_number(to_char(rq, 'yyyyMMdd')) - rownum DAYS
                  FROM (SELECT * FROM Test ORDER BY ID, rq) A) B
         GROUP BY B.ID, B.DAYS  order by B.ID


查询结果:


连续日期统计

你可能感兴趣的:(数据结构,sql)