求Oracle 统计连续天数查询语句样表如下:NAME RQA001 2020-01-01A001 2020-01-02 A001 2020-01-05A001 2020-01-06
A001 2020-01-07
B001 2020-01-01B001 2020-01-02
B001 2020-01-03
B001 2020-01-04
B001 2020-01-06想实现查询结果为:NAME 连续天数A001 2A001 3B001 4B001 1
这是一个非常经典的数据工程师面试题~需要利用到开窗函数和辅助列~我就不写了!给你思路自己研究研究~
1先按name开窗,给日期排序~给出序号值(辅助列)rw
2再用rq-辅助列值等到一个新的日期值re_rq
3再用rq-re_rq得到另外一个新值列now_2
4再按name,now_2分组求count(1) as now_3
5最后 按name分组求max(now_3)
https://bbs.fanruan.com/wenda/question1/82318.html
SELECT DISTINCT NAME ,COUNT(*) OVER (PARTITION BY NAME,B ) AS C FROM ( SELECT A,NAME,RQ,RQ1,RQ1-A AS B FROM ( SELECT ROW_NUMBER() OVER( ORDER BY NAME,RQ ) AS A ,NAME,RQ,SUBSTR(RQ,9) AS RQ1 FROM NT ) ) 我就做个标记,实践一下,记录下
select distinct name,min(rq) ksrq,max(rq) jsrq,count(*) cs
from
(
SELECT NAME,RQ,XH,to_char(to_date(rq,'yyyy-mm-dd')-xh,'yyyy-mm-dd') rq2
FROM
SELECT NAME,RQ,ROW_NUMBER()OVER(PARTITION BY NAME ORDER BY RQ) XH
FROM TEST20200319
)
group by name,rq2