Analyzing partition by consecutive packets sql

partition by ordered based classification field plus rownum may be formed each from a start reordering

For example, according to me based on the time, consecutive merged into one group, the number of statistics for each group in the interval in the

---------------------------------------------------

2010-07-18   2010-07-25   359
2010-06-13   2010-07-11   358
2010-06-06   2010-06-06   359
2010-05-16   2010-05-30   360

---------------------------------------------------

 

Can be implemented with the following code

Analog data

create table x (weekEndDate char(10), storeCount int);
insert into x values
('2010-07-25',359),
('2010-07-18',359),
('2010-07-11',358),
('2010-07-04',358),
('2010-06-27',358),
('2010-06-20',358),
('2010-06-13',358),
('2010-06-06',359),
('2010-05-30',360),
('2010-05-23',360),
('2010-05-16',360);

Ordered Group Statement

select min(weekenddate) as startdate, max(weekenddate) as enddate, min(storecount) as storecount 
from 
(select weekenddate, storecount, concat(row_number() over (order by weekenddate) -row_number() over (partition by storecount order by weekenddate),'|',storecount) as groupkey from x) w
group by groupkey order by startdate desc;

 According to ordinary sort order by ordering the partition partition by doing the sort subtraction can get a new grouping column, we know that according to this column to get the result we want 

Guess you like

Origin www.cnblogs.com/linyijia/p/11027694.html