I have this code:
insert into [dbo].[NGC_Agent_Intervals] SELECT AgentID, date, [Hour] = DATEPART(HOUR, RSRange), hour_quarter = CONCAT(DATEPART(MINUTE, RSRange) ,'-', DATEPART(MINUTE, RERange)), DepartmentID, [DepartmentName], [AgentState], [AgentStateReason], ( select AgentStateReasonDescription from RealRanges) as AgentStateReasonDescription, [ExtensionID], [WorkstationID], Duration = sum(CASE WHEN RSRange <= aStart and aend >= RERange THEN DATEDIFF(SECOND, aStart, DATEADD(MINUTE, 15, RSRange)) when RSRange <= aStart and aend < RERange THEN DATEDIFF(SECOND, aStart, aEnd) WHEN RERange >= aEnd THEN DATEDIFF(SECOND, RSRange, aEnd) ELSE DATEDIFF(SECOND, RSRange, RERange) END) FROM RealRanges WHERE DATEDIFF(SECOND, RSRange, aEnd) > 0 AND AgentState NOT IN ('logout', 'LOGIN') GROUP BY duration,RealRanges.AgentID, RealRanges.date, RealRanges.hour, DepartmentID,[DepartmentName], [AgentState], [AgentStateReason], [ExtensionID],[WorkstationID], RSRange, RealRanges.RERange
I run this code after using a cte to split the original rows from a table to a few rows by hour quarters. What I want to do is to sum the duration value, if a few columns are matching in a few rows.
The only problem is that I want to group by only for the following columns:
So what am I missing?