I'm hoping this is just an error in my formatting so i won't post more of my query than necessary.
I had a large query with multiple joins and several SUM arguments (the point of this query is to aggregate daily totals every time it gets ran).
This is my trouble line:
sum(case when (duration > 60 and LEGTYPE1 = 1) then 1 else 0 end)
I also tried this:
count(case when (duration > 60 and LEGTYPE1 = 1) then 1 else null end)
Here's the problem: The sum case returns either a 1 or 0, and the count line does return the count (13,14,16) but it's not the right number for my records.
Here's the whole query for context:
SELECT c.extension
, sum(Duration) AS Total_Talk_Time_seconds
, round(sum(Duration) / 60,2) AS Total_Talk_Time_minutes
, sum(if(LEGTYPE1 = 1,1,0)) AS Total_Outbound
, sum(if(LEGTYPE1 = 2,1,0) and ANSWERED = 1) AS Total_Inbound
, sum(if(Answered = 1,0,1)) AS Total_Missed
, SUM(IF(LEGTYPE1 = 1, 1, 0)) + -- outbound calls
SUM(IF(LEGTYPE1 = 2, 1, 0) AND ANSWERED = 1) + -- inbound calls
SUM(IF(Answered = 1, 0, 1)) AS Total_Calls
, NOW() AS Time_of_report
, curdate() AS Date_of_report
FROM cdrdb.session a
INNER JOIN cdrdb.callsummary b
ON a.NOTABLECALLID = b.NOTABLECALLID
INNER join cdrdb.mxuser c
ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
WHERE b.ts >= curdate()
AND c.extension IN (7276,7314,7295,7306,7357,7200,7218,7247,7331,7255,7330,7000,7215,7240,7358,7312)
group by c.extension
SO, when I do the count version of that line, the number matches the total number of calls, where it should only represent the outbound ones that last longer than 60 seconds. The data is correct and the fields are correct, I'm simply asking if my syntax is correct or if there's simply a better way to structure it.