Geoff_S
Geoff_S

Reputation: 5107

Combining conditions for CASE, mysql query

I'm trying to finish up a query to total different numbers for our people on the phones and I have the standard totals calculated fine, it seems, but I'm now trying to do this:

For every total column (total calls, total inbound, total outbound, total missed) I now need to have a new column partnered with it for known numbers in our database (so I'll have total calls known, total inbound known, etc.).

The way I'm doing this is by checking two fields in my session table: callingpartyno and finallycalledpartyno. So, for every call in session I need to check to see if the number in either of those fields are in my knownNumbers table in the phone_number field. If so, I need to count them for the known column.

About 8 lines down in my query below, I have my first instance of this which seems to be showing accurate numbers, but it's only incorporating callingpartyno. I first need to know how to combine conditions in this 'case whento addfinallycalledpartyno` as well.

The other issue is that for inbound and outbound, I need to base the phone number field I'm using on the LEGTYPE field. So, for example, IF I'm calculating outbound calls to known numbers I need something like

sum(if(LEGTYPE1 = 1,1,0)) AND finallycalledpartno = k.phone_number AS Total_Outbound_known.

I hope this makes sense, and it should be pretty straightforward for an advanced SQL programmer. I just can't seem to get the combination of conditions in a case when statement to work especially.

Query below:

SELECT u.firstn
      ,u.lastn
      ,c.extension
      ,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
      ,sum(case when CALLINGPARTYNO = k.phone_number  then 1 else 0 end ) AS total_known
      ,sum(if(Answered = 1,0,1)) AS Total_Missed
      ,sum(if(LEGTYPE1 = 2,1,0) and ANSWERED = 1) AS Total_Recieved
      ,sum(if(LEGTYPE1 = 1,1,0)) AS Total_Outbound
      ,round(sum(Duration) / 60,2) AS Total_Talk_Time_minutes
      ,sum(if(Answered = 1,0,1)) / (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)))  * 100 AS Percentage_Missed
FROM ambition.session a
INNER JOIN ambition.callsummary b
ON a.NOTABLECALLID = b.NOTABLECALLID
INNER join ambition.mxuser c
ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
INNER join jackson_id.users u
on c.extension = u.extension
left join ambition.knownnumbers k
on a.callingpartyno = k.phone_number
WHERE b.ts between curdate() - interval 5 day and now()
AND c.extension IN (7276,7314,7295,7306,7357,7200,7218,7247,7331,7255,7330,7000,7215,7240,7358,7312)
group by c.extension;

Upvotes: 0

Views: 47

Answers (1)

etsa
etsa

Reputation: 5060

Apart what I wrote in above comments, I would rewrite your query like this (I think at least it should be easier to read it, avoiding to repeat some SUMs)

SELECT firstn
    , lastn
    , extension
    , Total_Outbound+Total_Missed+Total_Received AS Total_Calls
    , Total_Known
    , Total_Missed
    , Total_Received
    , Total_Outbound
    , Total_Talk_Time_minutes
    , Total_Missed  / (Total_Outbound+Total_Missed+Total_Received)  * 100 AS Percentage_Missed
FROM (
      SELECT u.firstn
    , u.lastn
    , c.extension       
    , sum(case when CALLINGPARTYNO = k.phone_number  then 1 else 0 end ) AS Total_Known
    , sum(if(Answered = 1,0,1)) AS Total_Missed
    , sum(CASE WHEN LEGTYPE1 = 2 AND ANSWERED = 1 THEN 1 ELSE 0 END) AS Total_Received
    , sum(if(LEGTYPE1 = 1,1,0)) AS Total_Outbound
    , round(sum(Duration) / 60,2) AS Total_Talk_Time_minutes
      FROM ambition.session a
      INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
      INNER join ambition.mxuser c ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
      INNER join jackson_id.users u ON c.extension = u.extension
      LEFT JOIN ambition.knownnumbers k ON a.callingpartyno = k.phone_number
      WHERE b.ts between curdate() - interval 5 day and now()
      AND c.extension IN (7276,7314,7295,7306,7357,7200,7218,7247,7331,7255,7330,7000,7215,7240,7358,7312)
      GROUP BY c.extension, u.firstn, u.lastn  
     ) X;

Upvotes: 1

Related Questions