I have a table that goes like this:

Now i’m trying to get all called columns and a sum of the duration for a specific called var.
Now i’m trying this :
select called,SUM(duration) as dursum,time,CONVERT(VARCHAR(8), time, 4) AS Batch
from Calls
where caller='somevalue'
group by called,time
order by called
Now the problem is that i guess because i order by time as well i don’t really get distinct values by called, i get the same number several times, and ofcourse the sum acts the same, sums only part of the rows.
I can’t remove the group by time as it will cause an error for using the SUM function.
Again i want the result to have every called number once per day and the dursum to contain all the duration sum and grouped by the batch column somehow…
How can i solve this?
You are right, not grouping by time brings you the correct resultset. However, the error occuring then is easily explainable: it is caused by the selection of ” time,CONVERT(VARCHAR(8), time, 4) AS Batch ” in the select clause -> thus you just need some kind of aggregation for the time-field (for instance MAX(time),CONVERT(VARCHAR(8), MAX(time), 4) AS Batch.
Best regards