2
SELECT calldate, from_unixtime(FLOOR(UNIX_TIMESTAMP(calldate)/(60*60))*(60*60)) GroupTime
 , COUNT(*) as CntOut FROM asterisk.cdr 
 WHERE accountcode = '10102-131' AND DATE (calldate) = DATE (NOW()) AND calltype = 'outgoing' 
 GROUP BY GroupTime;

I have this query which groups a count for every hour of date for outgoing calls. Is there any way to get also the count of incoming as well in the same query?

3 Answers 3

1

Try this:

SELECT 
 Calldate
 from_unixtime(FLOOR(UNIX_TIMESTAMP(calldate)/(60*60))*(60*60)) GroupTime, 
 SUM(calltype='incoming') as CntIn, 
 SUM(calltype='outgoing') as CntOut 
FROM asterisk.cdr 
WHERE accountcode = '10102-131' 
  AND DATE (calldate) = DATE (NOW()) 
  AND calltype IN ('incoming', 'outgoing' )
GROUP BY GroupTime;

Sample SQL Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting similar results on all solutions only each row is not grouping. 2015-07-05 11:50:00 2015-07-05 11:00:00 0 1 2015-07-05 11:56:52 2015-07-05 11:00:00 0 1 2015-07-05 12:15:00 2015-07-05 12:00:00 0 1 2015-07-05 12:15:15 2015-07-05 12:00:00 0 1 2015-07-05 12:15:27 2015-07-05 12:00:00 0 1 2015-07-05 12:15:58 2015-07-05 12:00:00 0 1 2015-07-05 12:17:09 2015-07-05 12:00:00 0 1 2015-07-05 12:20:05 2015-07-05 12:00:00 0 1
@AD001 I've updated the query and Fiddle. Try removing the Calldate from the group by.
@AD001 There is some good additional information and tips in the answer by Gordon Linoff too that I didn't incorporate into this, but you should look at that too :)
1

Assuming that "outgoing" and "incoming" are descriptors on the call:

SELECT date(calldate), hour(calldate) as callhour,
       count(*) as Cnt,
       sum(calltype = 'outgoing') as CntOut,
       sum(calltype = 'incoming') as CntIn
FROM asterisk.cdr 
WHERE accountcode = '10102-131' AND
      (calldate >= curdate() and calldate < date_sub(curdate(), interval 1 day)
      ) AND
      calltype IN ('outgoing' , 'incoming')
GROUP BY date(calldate), hour(calldate);

Notes:

  • I find the hour() function easier to use to get the hour. However, it returns an integer rather than a time, so it may not be what you really want.
  • Using date() on calldate can prevent the use of an index. So, I replaced that logic with inequalities.
  • I assume you want separate counts for the outgoing and incoming calls.

Comments

0

If it is ok ot group by calltype, try:

SELECT 
   calltype 
   , calldate
   , from_unixtime(FLOOR(UNIX_TIMESTAMP(calldate)/(60*60))*(60*60)) GroupTime
   ,case when calltype='outgoing' then COUNT(*) else 0 end as CntOut 
   ,case when calltype='ingoing' then COUNT(*) else 0 end as CntIn 
 FROM asterisk.cdr 
 WHERE 
   accountcode = '10102-131' 
   AND DATE (calldate) = DATE (NOW()) 
   AND (calltype = 'outgoing' or calltype='ingoing')--if calltype in only 'ingoing' or 'outgoing', you can omit this line
 GROUP BY calltype, calldate;

But if not, the query would be a bit complicated:

SELECT 
   t.calldate
   , from_unixtime(FLOOR(UNIX_TIMESTAMP(t.calldate)/(60*60))*(60*60)) GroupTime
   , t_out.cnt as CntOut
   , t_in.cnt as CntIn
 FROM 
  asterisk.cdr as t
  left join (select calldate,count(*) cnt from asterisk.cdr where calltype='outgoing' group by calldate) as t_out
  on t.calldate=t_out.calldate
  left join (select calldate,count(*) cnt from asterisk.cdr where calltype='ingoing' group by calldate) as t_in
  on t.calldate=t_in.calldate
 WHERE 
   t.accountcode = '10102-131' 
   AND DATE (t.calldate) = DATE (NOW()) 
 GROUP BY  t.calldate;

2 Comments

just getting CntOut calltype, calldate, GroupTime, CntOut (would also then like the same count for inbound as well for the same groups)
Do you want the counts in separate columns?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.