В моем наборе данных много дел, и я хочу взглянуть на данные с другой точки зрения.Вот почему мне нужно указать начальный и конечный моменты, но как?
Я уже пытался определить минимальные и максимальные моменты как ручного, так и автоматического.Но как определить другие моменты начала и окончания?
UPDATE Test_Table
SET Activity = 'Start Manual'
FROM Test_Table b,
(
SELECT ID, MIN(rownumber) as a FROM Test_Table
WHERE Activity = 'Manual'
GROUP BY ID) a
WHERE b.rownumber <= a.a AND Activity = 'Manual'
and b.ID = a.ID
UPDATE Test_Table
SET Activity = 'End Manual'
FROM Test_Table b,
(
SELECT ID, MAX(rownumber) as a FROM Test_Table
WHERE Activity = 'Manual'
GROUP BY ID) a
WHERE b.rownumber >= a.a AND Activity = 'Manual'
and b.ID = a.ID
UPDATE Test_Table
SET Activity = 'Start Automatic'
FROM Test_Table b,
(
SELECT ID, MIN(rownumber) as a FROM Test_Table
WHERE Activity = 'Automatic'
GROUP BY ID) a
WHERE b.rownumber <= a.a AND Activity = 'Automatic'
and b.ID = a.ID
UPDATE Test_Table
SET Activity = 'End Automatic'
FROM Test_Table b,
(
SELECT ID, MAX(rownumber) as a FROM Test_Table
WHERE Activity = 'Automatic'
GROUP BY ID) a
WHERE b.rownumber >= a.a AND Activity = 'Automatic'
and b.ID = a.ID
Это пример значений таблицы:
ID Activity Datetime Rownumber Type_Activity
A Manual 2019-04-23 11:17:12 1 0
A Manual 2019-04-23 11:17:57 2 0
A Manual 2019-04-23 11:18:53 3 0
A Manual 2019-04-23 11:19:49 4 0
A Automatic 2019-04-23 11:26:50 5 1
A Automatic 2019-04-23 11:42:30 6 1
A Automatic 2019-04-23 11:43:31 7 1
A Automatic 2019-04-23 11:44:32 8 1
A Manual 2019-04-24 03:35:06 9 0
A Manual 2019-04-24 03:36:01 10 0
A Manual 2019-04-24 03:36:57 11 0
A Manual 2019-04-24 03:37:48 12 0
A Manual 2019-04-24 03:38:44 13 0
A Manual 2019-04-24 03:39:40 14 0
A Manual 2019-04-24 03:40:46 15 0
A Manual 2019-04-24 03:41:52 16 0
A Automatic 2019-04-24 04:26:50 17 1
A Automatic 2019-04-24 04:42:30 18 1
A Automatic 2019-04-24 04:43:31 19 1
A Automatic 2019-04-24 04:44:32 20 1
Я ожидал следующего:
ID Activity Datetime Activity2
A Manual 2019-04-23 11:17:12.000 Start Manual
A Manual 2019-04-23 11:17:57.000
A Manual 2019-04-23 11:18:53.000
A Manual 2019-04-23 11:19:49.000 End Manual
A Automatic 2019-04-23 11:26:50.000 Start Automatic
A Automatic 2019-04-23 11:42:30.000
A Automatic 2019-04-23 11:43:31.000
A Automatic 2019-04-23 11:44:32.000 End Automatic
A Manual 2019-04-24 03:35:06.000 Start Manual
A Manual 2019-04-24 03:36:01.000
A Manual 2019-04-24 03:36:57.000
A Manual 2019-04-24 03:37:48.000
A Manual 2019-04-24 03:38:44.000
A Manual 2019-04-24 03:39:40.000
A Manual 2019-04-24 03:40:46.000
A Manual 2019-04-24 03:41:52.000 End Manual
A Automatic 2019-04-24 04:26:50 Start Automatic
A Automatic 2019-04-24 04:42:30
A Automatic 2019-04-24 04:43:31
A Automatic 2019-04-24 04:44:32 End Automatic