Hello community!
There is a T-SQL that I cannot figure it out how to do it, hope you can point me in the right direction or what would be the best approach (in terms of performance).
Using cursors is not a good idea since the amout of data will be heavy in production.
An industrial application will be populating a table every second during certain time. 2 variables (combinations of filter and tank) are key in order to "group" the data event. Also, an exception would be if time exceeds certain time (in this example "newgroup" knows when a new datagroup should be considered). Later on, there will be a process in charge to detect the "goup events" that were inserted in the database.
Values of "desired_datagroup_result_example" don't matter (but they need to be unique: NEWID() might be better than unique numeric values), the idea that I try to show is that the t-sql should throw 8 groups for this dataset example.
DECLARE @data TABLE(id int, newgroup bit, idfilter tinyint, idtank tinyint, created datetime, desired_datagroup_result_example int);
INSERT INTO @data VALUES
(3541,0,2,5,'2024-07-01 08:00:01.000',102),(1740,1,1,3,'2024-07-01 08:00:01.000',100),(1739,0,1,3,'2024-07-01 08:00:02.000',100),(3540,0,2,5,'2024-07-01 08:00:02.000',102),(3539,0,2,5,'2024-07-01 08:00:03.000',102),
(1738,0,1,3,'2024-07-01 08:00:03.000',100),(1737,0,1,3,'2024-07-01 08:00:04.000',100),(3538,0,2,5,'2024-07-01 08:00:04.000',102),(3537,0,2,5,'2024-07-01 08:00:05.000',102),(5342,0,2,1,'2024-07-01 09:00:01.000',103),
(5341,0,2,1,'2024-07-01 09:00:02.000',103),(5340,0,2,1,'2024-07-01 09:00:03.000',103),(5339,0,2,1,'2024-07-01 09:00:04.000',103),(5338,0,2,1,'2024-07-01 09:00:05.000',103),(5337,0,2,1,'2024-07-01 09:00:06.000',103),
(5336,0,2,1,'2024-07-01 09:00:07.000',103),(5335,0,2,1,'2024-07-01 09:00:08.000',103),(5334,1,2,1,'2024-07-01 09:16:09.000',104),(5333,0,2,1,'2024-07-01 09:16:10.000',104),(5332,0,2,1,'2024-07-01 09:16:11.000',104),
(5331,0,2,1,'2024-07-01 09:16:12.000',104),(5344,0,2,5,'2024-07-01 10:00:01.000',105),(5343,1,1,3,'2024-07-01 10:00:01.000',101),(5345,0,1,3,'2024-07-01 10:00:02.000',101),(5346,0,2,5,'2024-07-01 10:00:02.000',105),
(5347,0,2,5,'2024-07-01 10:00:03.000',105),(5348,0,1,3,'2024-07-01 10:00:03.000',101),(5349,0,1,3,'2024-07-01 10:00:04.000',101),(5350,0,2,5,'2024-07-01 10:00:04.000',105),(5351,0,2,5,'2024-07-01 10:00:05.000',105),
(5352,0,2,1,'2024-07-01 11:00:01.000',106),(5353,0,2,1,'2024-07-01 11:00:02.000',106),(5354,0,2,1,'2024-07-01 11:00:03.000',106),(5355,0,2,1,'2024-07-01 11:00:04.000',106),(5356,0,2,1,'2024-07-01 11:00:05.000',106),
(5357,0,2,1,'2024-07-01 11:00:06.000',106),(5358,0,2,1,'2024-07-01 11:00:07.000',106),(5359,0,2,1,'2024-07-01 11:00:08.000',106),(5360,1,2,1,'2024-07-01 11:16:09.000',107),(5361,0,2,1,'2024-07-01 11:16:10.000',107),
(5362,0,2,1,'2024-07-01 11:16:11.000',107),(5363,0,2,1,'2024-07-01 11:16:12.000',107);
SELECT * FROM @data
ORDER BY idfilter ASC, created ASC;
Edit: Forgot to mention the MSSQL version: SQL2019 standard edition x64
Any ideas are appreciated.
Cheers,