Group data into 4 hour interval and dont sum or avg a column - sql server script
Hello, I have this query that has data for every minute which I have attached some data of the table.
I want to group this into 4 hours starting from 00:00, but I don't want to sum or average the value in the column [Last] I just want to return that value in the [Last] column as it is at that specific grouped hour.
SELECT b.[timestamp], b.[id], b.[Date], b.[HourMin], b.[Min], b.[Last]FROM [vw_BassLineDataTimestamp] b
Notice: MSSQL keeps results in the same data type, so dividing an int by another int does not result in a float, this doesn't need Round(DateDiff(minute,0, somedatetime)/intervalleng
Now it's a bit unclear what you want, do you want the value of [Last] as it is in the last record of the group? Then you need to look for the record with the max(somedatetime) within that group (that's received by grouping) and pick out the full record of that somedateetime=maxsomedatet
That could be done with window function and partitioning using LAST_VALUE ( [Last]) OVER ( partition by DateDiff(minute,0, somedatetime)/intervalleng