Схема (MySQL v8.0)
CREATE TABLE t1 (
`userid` INTEGER,
`timestamp` VARCHAR(19),
`fruit` VARCHAR(6)
);
INSERT INTO t1
(`userid`, `timestamp`, `fruit`)
VALUES
('1', '2018-12-10T14:46:50', 'banana'),
('1', '2018-12-10T15:46:50', 'banana'),
('1', '2018-12-10T16:46:50', 'apple'),
('1', '2018-12-10T17:46:50', 'banana');
Запрос № 1
Простой подход, если вы не возражаете против несколькихзаписи для последовательных фруктов
select userid, fruit, timestamp `start`,
lead(timestamp) over (order by timestamp) `end`
from t1;
| userid | fruit | start | end |
| ------ | ------ | ------------------- | ------------------- |
| 1 | banana | 2018-12-10T14:46:50 | 2018-12-10T15:46:50 |
| 1 | banana | 2018-12-10T15:46:50 | 2018-12-10T16:46:50 |
| 1 | apple | 2018-12-10T16:46:50 | 2018-12-10T17:46:50 |
| 1 | banana | 2018-12-10T17:46:50 | |
Или Запрос № 2
SELECT t2.*
FROM (
SELECT userid,
fruit,
timestamp `tstart`,
CASE
WHEN fruit = Lead(fruit) over(ORDER BY timestamp) THEN lead(timestamp, 2) over ( ORDER BY timestamp)
ELSE lead(timestamp, 1) over ( ORDER BY timestamp)
end `tend`,
CASE
WHEN fruit = lag(fruit) over (ORDER BY timestamp) THEN 1
ELSE 0
end del
FROM t1 ) t2
WHERE del = 0;
| userid | fruit | tstart | tend | del |
| ------ | ------ | ------------------- | ------------------- | --- |
| 1 | banana | 2018-12-10T14:46:50 | 2018-12-10T16:46:50 | 0 |
| 1 | apple | 2018-12-10T16:46:50 | 2018-12-10T17:46:50 | 0 |
| 1 | banana | 2018-12-10T17:46:50 | | 0 |
Просмотр в БД Fiddle