I have these sample records in my table, I want to group them by every 3 minutes.based on min(Matched_BeginTime) on each group.
Keywords Matched_BeginTime Station_ID Program_ID TVCapCID ScheduleID
"Police" 2015-11-17 17:00:37.160 120 715 486290066 102691
"Police" 2015-11-17 17:01:48.620 120 715 486290088 102691
"Police" 2015-11-17 17:02:02.140 120 715 486290091 102691
"Police" 2015-11-17 17:02:53.100 120 715 486290102 102691
"Police" 2015-11-17 17:03:03.540 120 715 486290106 102691
"Police" 2015-11-17 17:03:48.020 120 715 486290119 102691
"SAS" 2015-11-17 17:03:57.940 120 715 486290122 102691
"SAS" 2015-11-17 17:08:25.800 120 715 486290197 102691
"Police" 2015-11-17 17:08:32.960 120 715 486290199 102691
"Emergency" 2015-11-17 17:09:02.540 120 715 486290206 102691
"SAS" 2015-11-17 17:10:55.640 120 715 486290246 102691
"Police" 2015-11-17 17:12:23.240 120 715 486290273 102691
this is my ideal result:
Keywords_Groups Min_Matched_BeginTime
Police 11/17/2015 17:00:37
Police,SES 11/17/2015 17:03:48
SES,Police,Emergency 11/17/2015 17:08:26
Police 11/17/2015 17:12:23
Each record is grouped by matched_Date in every 3 minutes .
forexp , first 5 records should be in one group because theyre in range of min(matchDate)+ 3minutes
Note : I tried to use the solutions given before. like these ones:
How to group time by hour or by 10 minutesSQL SERVER - Group records by n minutes interval
this is the sample code I tried from above solutions:
select datepart(minute, Matched_BeginTime)/3,min(Matched_BeginTime),Keywords
from [table]
group by datepart(minute, Matched_BeginTime)/3,Keywords
order by MIN(Matched_BeginTime)
and This is the result :
0 2015-11-17 17:00:37.160 "Police"
1 2015-11-17 17:03:03.540 "Police"
1 2015-11-17 17:03:57.940 "SAS"
2 2015-11-17 17:08:25.800 "SAS"
2 2015-11-17 17:08:32.960 "Police"
3 2015-11-17 17:09:02.540 "Emergency"
3 2015-11-17 17:10:55.640 "SAS"
4 2015-11-17 17:12:23.240 "Police"
As you can see, it grouped by minutes division by 3 , started from 00 but not based on Min(Matched_BeginTime)
Thank you for any help
Note:By using Stored Procedure, I can do it fine, I prefer to have a single TSQL command for that.