The problem of remembering the SQL time series grouping summary

Source: Internet
Author: User

Implement date interval grouping, interval less than or equal to 3s data is a group, the data source is as follows

CREATE table #tmptable (ID nvarchar (), DD date, dt datetime) Goinsert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:01 Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:02 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:03 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:04 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:05 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:06 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ') , ' 2010-1-1 00:00:07 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:09 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:11 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:12 ') Insert #tmptable values (' 1 ' , ' 2010-1-1 ', ' 2010-1-1 00:00:15 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:19 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:20 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:24 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:25 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:26 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:27 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:28 ') Insert #tmptable values (' 1 ', ' 2010-1-1 ', ' 2010-1-1 00:00:29 ') Insert #tmptable values (' 1 ', ' 2010-1-2 ', ' 2010-1-2 00:00:36 ') Insert #tmptable values (' 1 ', ' 2010-1-2 ', ' 2010-1-2 00:00:37 ') Insert #tmptable values (' 1 ', ' 2010-1-2 ') , ' 2010-1-2 00:00:48 ') Insert #tmptable values (' 1 ', ' 2010-1-2 ', ' 2010-1-2 00:00:59 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:09 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:11 ') Insert #tmptable values (' 2 ' , ' 2010-1-1 ', ' 2010-1-1 00:00:12 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:15 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:19 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:20 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:24 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:25 ') Insert #tmptable VALUES (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:26 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-100:00:27 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:28 ') Insert #tmptable values (' 2 ', ' 2010-1-1 ', ' 2010-1-1 00:00:29 ') Insert #tmptable values (' 2 ', ' 2010-1-2 ', ' 2010-1-2 00:00:36 ') Insert #tmptable values (' 2 ', ' 2010-1-2 ') , ' 2010-1-2 00:00:37 ') Insert #tmptable values (' 2 ', ' 2010-1-2 ', ' 2010-1-2 00:00:48 ') Insert #tmptable values (' 2 ', ' 2010-1-2 ', ' 2010-1-2 00:00:59 ') go

Detecting data breakpoints

; With MyList as (select Row_number () Up (ORDER by ID,DD, DT ASC) rn,t.* from  #tmptable T) SELECT A.rn,a.id,a.dd,a.dt,ca SE when b.dt are null then 0ELSE DATEDIFF (SS,B.DT,A.DT) END as secspan,case when B.DT is null or DATEDIFF (SS,B.DT,A.DT) < ; = 3 then 1 ELSE 0 END as Flag--into #TMP1 from MyList a left join MyList bon b.rn = a.rn-1 and a.id = b.ID and A.dd
   = B.DD

 

Aggregation time period

Select Id,dd,min (DT) as Sdt,max (DT) as EDT from (Select ss= (select SUM (Flags) from #TMP1 where DT <= a.dt and id = a.id and DD = a.dd), * from #TMP1 a where id= a.id and dd = a.dd) Agroup by (RN-SS), ID,DD

 

 

The problem of remembering the SQL time series grouping summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.