Answer the question
In order to leave comments, you need to log in
how to calculate sequence using oracle?
Hey! I have a plate, it is necessary to count how many times a zero value was repeated in a row.
DAY_ID ARTICLE QTY
01.01.2014 000032 1
02.01.2014 000032 0
03.01.2014 000032 0
04.01.2014 000032 1
05.01.2014 000032 0
06.01.2014 000032 0
07.01.2014 000032 0
08.01.2014 000032 1
09.01.2014 000032 1
10.01.2014 000032 0
11.01.2014 000032 0
12.01.2014 000032 1
13.01.2014 000032 0
DAY_ID ARTICLE QTY streak
01.01.2014 000032 1
02.01.2014 000032 0 1
03.01.2014 000032 0 2
04.01.2014 000032 1
05.01.2014 000032 0 1
06.01.2014 000032 0 2
07.01.2014 000032 0 3
08.01.2014 000032 1
09.01.2014 000032 1
10.01.2014 000032 0 1
11.01.2014 000032 0 2
12.01.2014 000032 1
13.01.2014 000032 0 1
Answer the question
In order to leave comments, you need to log in
RANK() and DENSE_RANK()
functions www.sql-tutorial.ru/ru/book_rank_dense_rank_functi...
with tab1 as (
select '01.01.2014' as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select '02.01.2014' as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select '03.01.2014' as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select '04.01.2014' as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select '05.01.2014' as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select '06.01.2014' as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select '07.01.2014' as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select '08.01.2014' as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select '09.01.2014' as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select '10.01.2014' as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select '11.01.2014' as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select '12.01.2014' as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select '13.01.2014' as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual
)
select DAY_ID,ARTICLE, QTY,
(count(1) OVER (PARTITION BY QTY, tmp order by DAY_ID)) as streak
from
(
select DAY_ID,ARTICLE,QTY,
(
rownum - (SUM(1) OVER (PARTITION BY QTY order by DAY_ID))
) AS tmp
from tab1
order by DAY_ID
)
order by DAY_ID
;
DAY_ID ARTICLE QTY STREAK
01.01.2014 000032 1 1
02.01.2014 000032 0 1
03.01.2014 000032 0 2
04.01.2014 000032 1 1
05.01.2014 000032 0 1
06.01.2014 000032 0 2
07.01.2014 000032 0 3
08.01.2014 000032 1 1
09.01.2014 000032 1 2
10.01.2014 000032 0 1
11.01.2014 000032 0 2
12.01.2014 000032 1 1
13.01.2014 000032 0 1
with tab1 as (
select to_date('01.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select to_date('02.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select to_date('03.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select to_date('04.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select to_date('05.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select to_date('06.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select to_date('07.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select to_date('08.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select to_date('09.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select to_date('10.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select to_date('11.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual union all
select to_date('12.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 1 as QTY from dual union all
select to_date('13.01.2014','dd.mm.yyyy') as DAY_ID, '000032' as ARTICLE, 0 as QTY from dual
)
select
t.DAY_ID
,t.ARTICLE
,t.QTY
,decode(qty,0,count(*) over(partition by s order by day_id)) zero_n
from
(
select
tab1.*
,row_number()over(order by day_id) - decode(qty,0,count(*)over(partition by qty order by day_id)) s
from tab1
) t
order by day_id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question