See table dataHow to count the number of days in MYsql where an event has happened in a row consecutively daily?
CREATE TABLE MrDataConverter (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
num INT,
controller_id INT,
event_type INT,
date VARCHAR(255),
Desired output INT
);
INSERT INTO MrDataConverter
(num,controller_id,event_type,date,Desired output)
VALUES
(1,42260,12,'2/27/2023 13:09',8),
(2,42260,12,'2/26/2023 13:09',7),
(3,42260,12,'2/25/2023 13:09',6),
(4,42260,12,'2/24/2023 13:08',5),
(5,42260,12,'2/23/2023 13:08',4),
(6,42260,12,'2/22/2023 13:06',3),
(7,42260,12,'2/21/2023 13:09',2),
(8,42260,12,'2/20/2023 13:06',1),
(9,42260,12,'2/19/2023 10:27',0),
(10,42260,12,'2/19/2023 3:11',0),
(11,42260,12,'2/17/2023 16:08',0),
(12,42260,12,'2/17/2023 15:36',0),
(13,42260,12,'2/17/2023 14:20',0),
(14,42260,12,'2/17/2023 13:14',9),
(15,42260,12,'2/16/2023 13:05',8),
(16,42260,12,'2/15/2023 13:04',7),
(17,42260,12,'2/14/2023 13:04',6),
(18,42260,12,'2/13/2023 13:05',5),
(19,42260,12,'2/12/2023 13:05',4),
(20,42260,12,'2/11/2023 13:05',3),
(21,42260,12,'2/10/2023 13:04',2),
(22,42260,12,'2/9/2023 13:04',1),
(23,42260,12,'2/8/2023 13:04',0);
I assume I need to do a datediff ( from the previous row ) and Row_number function?
SELECT row_number() over ( PARTITION BY controller_id ORDER BY date DESC) num, controller_id, date
FROM events
WHERE controller_id = 42260
Order By date desc
LIMIT 200