So, I have a table:
DATE STORE PRODUCT
01.01.2020 Store1 Product1
01.01.2020 Store1 Product2
01.01.2020 Store1 Product3
01.01.2020 Store1 Product4
01.02.2020 Store1 Product5
01.02.2020 Store1 Product6
01.02.2020 Store1 Product7
01.02.2020 Store1 Product8
01.01.2020 Store2 Product1
01.01.2020 Store2 Product2
01.01.2020 Store2 Product3
01.01.2020 Store2 Product4
03.01.2020 Store2 Product1
01.03.2020 Store2 Product2
02.03.2020 Store2 Product8
03.03.2020 Store2 Product10
06.08.2020 Store2 Product6
08.11.2020 Store2 Product7
I need to to Count Distinct stores. Moreover, if more than 20 days have passed between previous rows, then the value of store is unique
What I expect:
date Store Product Product_x
01.01.2020 Store1 Product1 1
01.01.2020 Store1 Product2 1
01.01.2020 Store1 Product3 1
01.01.2020 Store1 Product4 1
01.02.2020 Store1 Product5 1e
01.02.2020 Store1 Product6 1e
01.02.2020 Store1 Product7 1e
01.02.2020 Store1 Product8 1e
01.01.2020 Store2 Product1 2
01.01.2020 Store2 Product2 2
01.01.2020 Store2 Product3 2
01.01.2020 Store2 Product4 2
03.01.2020 Store2 Product1 2
01.03.2020 Store2 Product2 2e
02.03.2020 Store2 Product8 2e
03.03.2020 Store2 Product10 2e
06.08.2020 Store2 Product6 2ee
08.11.2020 Store2 Product7 2eee
What I have and my query:
DATE_ STORE PRODUCT TT
01.01.2020 Store1 Product1 1
01.01.2020 Store1 Product2 1
01.01.2020 Store1 Product3 1
01.01.2020 Store1 Product4 1
01.02.2020 Store1 Product8 1e
01.02.2020 Store1 Product7 1
01.02.2020 Store1 Product5 1
01.02.2020 Store1 Product6 1
01.01.2020 Store2 Product4 2
01.01.2020 Store2 Product3 2
01.01.2020 Store2 Product2 2
01.01.2020 Store2 Product1 2
03.01.2020 Store2 Product1 2
01.03.2020 Store2 Product2 2e
02.03.2020 Store2 Product8 2
03.03.2020 Store2 Product10 2
06.08.2020 Store2 Product6 2e
08.11.2020 Store2 Product7 2e
Query:
select p.*, case
when p.date_ - lag(p.date_, 1) over ( partition by p.store order by p.date_)>20
then to_char(dense_rank() over (order by p.store)) || 'e'
else to_char(dense_rank() over (order by p.store)) end tt
from table p;
How is it possible to solve this problem? And how to apply a recursive function to add "e" if the difference between the dates is more than 20 days?
From Oracle 12, you can use
MATCH_RECOGNIZEfor row-by-row pattern matching:Or, in earlier versions, using the
LAGandSUManalytic functions:Which, for the sample data:
Both output:
fiddle
You probably could do it with recursion but it is likely going to be harder than using
MATCH_RECOGNIZEor alternatively,LAG/LEADand analytic functions.