오래된 질문, 내가 아는,하지만 여전히 누군가 도움이 될 수 있습니다 ...
는이 문제를 해결하기는, 내가 날짜가 순차적 것으로 가정했다. 그런 다음 데이터를 '어제'데이터로 연결 한 다음 '내일의 데이터'로 연결 한 다음 플래그 값이 변경된 행을 가져 왔습니다.이 표시는 해당 계열의 시작일을 &으로 표시합니다. 시작일을 종료일과 일치 시키려면 각 시리즈를 주문하고 카운터를 할당하고이 카운터에 가입하십시오.
SET @cnt1 := 0;
SET @cnt2 := 0;
select firstdate, lastdate, startDates.flag from
(
select @cnt2 := @cnt2 + 1 as i, table1.date as firstdate, table1.flag from table1
left join (
select DATE_ADD(date, INTERVAL 1 DAY) as dateplus1, date, flag from table1
) plus1 on table1.date = plus1.dateplus1
where table1.flag <> IFNull(plus1.flag,-1)
order by table1.date
) startDates
inner join
(
select @cnt1 := @cnt1 + 1 as i, table1.date as lastdate, table1.flag from table1
left join (
select DATE_ADD(date, INTERVAL -1 DAY) as dateminus1, date, flag from table1
) minus1 on table1.date = minus1.dateminus1
where table1.flag <> IFNull(minus1.flag,-1)
order by table1.date
) endDates on startDates.i = endDates.i
;
SQL 바이올린 여기 : - 매우 유사 http://sqlfiddle.com/#!2/25594/1/2
참조 http://stackoverflow.com/questions/3410687/sql-group-by-on-consecutive-records 여기
코드입니다 질문 –원하는 출력의 기초가 무엇인지 명확하게 지정할 수 있습니까? –