I have a table in the below structure.
phone bigint
answer_time datetime
I should group all phone numbers, and its total number of attempts and mention its no:of time answered and no:of time not answered. For this the condition is answer_time='0000-00-00 00:00:00' for not answered and answer_time > '0000-00-00 00:00:00' for answered. There can be any number of times the call has been made to a particular number. Any suggestions or approach would be helpful. I'm terribly stuck up on this.
The result should be :
+----------------+---------------+--------------------+--
| phone | count(*) | NOT_ANSWERED | ANSWERED |
+---------------+----------+------------+------------+-----+
| 7260513 | 2 | 1 | 1 |
+------------+----------+------------+--------------------+
In this, at the first attempt the call is answered at the second it is not. The attempts may range max to 16 times.
FOR MAX
mysql> select phone, answer_time from cdr_test;
+------------+---------------------+
| phone | answer_time |
+------------+---------------------+
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
| 31389966 | 0000-00-00 00:00:00 |
+------------+---------------------+
13 rows in set (0.00 sec)
mysql> select phone, Count(*), Count(CASE WHEN Time(answer_time) = 0 THEN 1 end ) AS NOT_ANSWERED, Count(CASE WHEN Time(answer_time) > 0 THEN 1 end) AS ANSWERED FROM cdr_test GROUP BY phone;
+------------+----------+--------------+----------+
| phone | Count(*) | NOT_ANSWERED | ANSWERED |
+------------+----------+--------------+----------+
| 31389966 | 13 | 0 | 0 |
+------------+----------+--------------+----------+
1 row in set (0.00 sec)
