drop table test
/
create table test
(
id varchar2(20) not null,
day varchar2(20) not null,
value varchar2(20) not null
)
/
insert into test values('1', '20080309', 'b');
insert into test values('1', '20080306', 'b');
insert into test values('1', '20080305', 'b');
insert into test values('1', '20080304', 'a');
insert into test values('1', '20080303', 'a');
insert into test values('2', '20080309', 'b');
insert into test values('2', '20080306', 'b');
insert into test values('2', '20080305', 'a');
insert into test values('2', '20080304', 'a');
insert into test values('2', '20080303', 'a');
insert into test values('3', '20080309', 'a');
insert into test values('3', '20080306', 'b');
insert into test values('3', '20080305', 'b');
insert into test values('3', '20080304', 'a');
insert into test values('3', '20080303', 'a');
commit;
select id,count(*)
from (
select test.*
,row_number() over(partition by id order by day desc) rn
from test where value='https://bbs.csdn.net/topics/b' and to_date(day,'yyyy-mm-dd')<=date '2008-03-30'
)
where to_date(day,'yyyy-mm-dd')+rn = date '2008-03-09' +1
group by id;
日期有間隔是因為周六周日沒資料,想通過計算3月9日value=https://bbs.csdn.net/topics/b的連續出現次數,得到資料如下:
id count(*)
1 1
2 1
請問應該怎么寫sql
uj5u.com熱心網友回復:
頂一下~~~~~~~~~~uj5u.com熱心網友回復:
對于資料庫來說 ,資料之間是不存在連續性的,你只有用過日期欄位判斷,日期連續無非就是后一天減去前一天都等于1,這種復雜的sql估計最好用存盤程序游標實作,邏輯是:先select order by value day,再進行分組partition by,再開啟游標,用后一行減去前一行的日期,直到值大于1則關閉游標,這樣次數就出來了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/69708.html
標籤:開發
上一篇:Oracle資料庫匯入資料驗證
