Salve,
c'e' una soluzione matematica interessante su
https://stackoverflow.com/questions/5002661/how-to-group-time-by-hour-or-by-10-minutes...
brutalmente, visto la tabella {Id (identity), Data}, con popolazione
VALUES ( '2020-01-01 10:00:01' ), ( '2020-01-01 10:01:15' ), ( '2020-01-01 10:02:30' ), ( '2020-01-01 10:03:45' ), ( '2020-01-01 10:04:00' )
, ( '2020-01-01 10:05:01' ), ( '2020-01-01 10:06:15' ), ( '2020-01-01 10:07:30' ), ( '2020-01-01 10:08:45' ), ( '2020-01-01 10:09:00' )
, ( '2020-01-01 10:55:01' ), ( '2020-01-01 10:56:15' ), ( '2020-01-01 10:57:30' ), ( '2020-01-01 10:58:45' ), ( '2020-01-01 10:59:00' )
, ( '2020-01-01 11:01:01' ), ( '2020-01-01 10:03:15' ), ( '2020-01-01 11:05:30' ), ( '2020-01-01 11:08:45' ), ( '2020-01-01 11:09:00' )
(non metto codice di insert per non causare problemi di filtro sul forum)
con filtri passati via codice per la data e il timeFrame, se ti ho ben capito potremmo fare qualche cosa simile a
DECLARE @data datetime = '2020-01-01';
DECLARE @frame int = 3; -- 3 minuti
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, '2000', [Data]) / @frame * @frame, '2000') AS [date_truncated]
, COUNT(*) AS [records_in_interval]
, MIN([Data]) [TimeStampFrame]
, MIN(Id) [IdTimeStampFrame]
FROM dbo.t
WHERE DATEADD(dd, DATEDIFF(dd, 0, Data), 0) = @data
GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, '2000', [Data]) / @frame * @frame, '2000')
ORDER BY [date_truncated];
WITH cte AS (
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, '2000', [Data]) / @frame * @frame, '2000') AS [date_truncated]
, COUNT(*) AS [records_in_interval]
, MIN([Data]) [TimeStampFrame]
, MIN(Id) [IdTimeStampFrame]
FROM dbo.t
WHERE DATEADD(dd, DATEDIFF(dd, 0, Data), 0) = @data
GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, '2000', [Data]) / @frame * @frame, '2000')
)
SELECT cte.IdTimeStampFrame, cte.TimeStampFrame
FROM cte
ORDER BY cte.IdTimeStampFrame;
-------------
-- primo set omissis
-- set finale di presentazione
IdTimeStampFrame TimeStampFrame
---------------- -----------------------
1 2020-01-01 10:00:01.000
4 2020-01-01 10:03:15.000
7 2020-01-01 10:06:15.000
10 2020-01-01 10:09:00.000
11 2020-01-01 10:55:01.000
13 2020-01-01 10:57:30.000
16 2020-01-01 11:01:01.000
18 2020-01-01 11:05:30.000
19 2020-01-01 11:08:45.000
20 2020-01-01 11:09:00.000
salutoni romagnoli
--
Andrea