title: aggregating by grouping, not just one record of totals
body: <p>I have a query below that should be totaling up numbers by CSR agent using their extension. Currently it's returning one line/record for one agent and her extension but it's showing the correct totals for all 15 agents on that one record.</p>
  
  <p>However, what I want is for it to list each agent and their extension with their own respective totals for the day. I'm grouping by extension but it doesn't seem to fix the issue.</p>
  
  <p>Is there something in my query that would stick out as a reason for it not breaking my totals up per extension/agent?</p>
  
  <pre><code>SELECT  firstn
  , lastn
  , extension
  , Recieved
  , RecievedKnown
  , Outbound
  , outboundKnown
  , Missed
  , MissedKnown
  , duration
  , HOLDTIMESECS
  
  
      FROM (
    SELECT firstn
     , lastn
     , c.extension
     , sum(CASE WHEN LEGTYPE1 = 2 AND ANSWERED = 1 THEN 1 ELSE 0 END) AS Recieved
     , sum(case when LEGTYPE1 = 2 and answered = 1 and CALLINGPARTYNO = k.phone_number then 1 ELSE 0 end) as RecievedKnown
     , sum(CASE WHEN ANSWERED = 1 AND LEGTYPE1 = 1 then 1 ELSE 0 end)  AS Outbound
     , sum(case when LEGTYPE1 = 1 and FINALLYCALLEDPARTYNO = k.phone_number then 1 ELSE 0 end) as outboundKnown
     , sum(case when Answered = 0 then 1 ELSE 0 end) as Missed
     , sum(case when ANSWERED = 0 and CALLINGPARTYNO = k.phone_number then 1 ELSE 0 end)   as MissedKnown
     , sum(b.duration) as duration
     , sum(a.holdtimesecs) as holdtimesecs
  
  
    FROM ambition.session a
    INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
    right join jackson_id.users c on a.callingpartyno = c.extension or a.finallycalledpartyno = c.extension
    LEFT JOIN ambition.known_numbers k ON a.callingpartyno = k.phone_number
    WHERE a.ts &gt;= curdate()
  
    and(a.CALLINGPARTYNO in (select extension from ambition.ambition_users) OR a.finallycalledpartyno IN (select extension from ambition.ambition_users))
    ) x
    group by extension;
  </code></pre>