Есть запрос:
SELECT t1.calldate,
t2.answered,
t3.no_answer
FROM
(SELECT DISTINCT(DATE(calldate)) AS calldate
FROM calls c
) AS t1
INNER JOIN (SELECT DISTINCT(DATE(calldate)) AS calldate,
COUNT(ID) AS answered
FROM calls
WHERE disposition = 1
AND did = '555'
AND MONTH(calldate) = 08
AND YEAR(calldate) = 2017
GROUP BY 1
) t2 ON t1.calldate = t2.calldate
INNER JOIN (SELECT DISTINCT(DATE(calldate)) AS calldate,
COUNT(ID) AS no_answer
FROM calls
WHERE disposition <> 1
AND did = '555'
AND MONTH(calldate) = 08
AND YEAR(calldate) = 2017
GROUP BY 1
) AS t3 ON t1.calldate = t3.calldate;
Выводит t1.calldate и t2.answered (если брать отдельно), а вот t3.no_answer ничего. В базе данных нет значений где disposition <> 1. А как сделать чтобы вместо "ничего" выводились нули? Всем заранее большое спасибо!
Используйте IFNULL:
IFNULL(t3.no_answer, "null")
или
IFNULL(t3.no_answer, 0)
http://www.mysqltutorial.org/mysql-ifnull/
SELECT DATE(calldate) AS calldate,
SUM( disposition = 1
AND did = '555'
AND MONTH(calldate) = 08
AND YEAR(calldate) = 2017) AS answered,
SUM( disposition <> 1
AND did = '555'
AND MONTH(calldate) = 08
AND YEAR(calldate) = 2017) AS no_answer
FROM calls
GROUP BY DATE(calldate)
Сам разобрался) Проблема была в том, что я использовал полное соединение (INNER JOIN) вместо LEFT JOIN, в том месте где нет данных)
SELECT t1.calldate, t2.answered, IFNULL(t3.no_answer,0) AS no_answer FROM(SELECT DISTINCT(DATE(calldate)) AS calldate FROM calls c) AS t1
INNER JOIN(SELECT DISTINCT(DATE(calldate)) AS calldate, COUNT(ID) AS answered FROM calls WHERE disposition = 1 AND did = '555' AND MONTH(calldate) = 08 AND YEAR(calldate) = 2017 GROUP BY 1) t2 ON t1.calldate = t2.calldate
LEFT JOIN(SELECT DISTINCT(DATE(calldate)) AS calldate, COUNT(ID) AS no_answer FROM calls WHERE disposition <> 1 AND did = '555' AND MONTH(calldate) = 08 AND YEAR(calldate) = 2017 GROUP BY 1) AS t3 ON t1.calldate = t3.calldate;
Как минимум, третий запрос не нужен, можно сделать через sum
подсчёт количества. Как я понял, для каждой даты вам нужно количество звонков. INNER JOIN
соединяет только там, где есть звонки с указанными условиями. Используйте LEFT JOIN
с ifnull
в итоговом select
:
SELECT t1.calldate,
ifnull(t2.answered, 0) as answered,
ifnull(t2.no_answer, 0) as no_answer
FROM
(SELECT DISTINCT(DATE(calldate)) AS calldate
FROM calls c
) AS t1
LEFT JOIN (SELECT DISTINCT(DATE(calldate)) AS calldate,
SUM(if(disposition = 1, 1, 0)) AS answered,
SUM(if(disposition <> 1, 1, 0)) as no_answer
FROM calls
WHERE did = '555'
AND MONTH(calldate) = 08
AND YEAR(calldate) = 2017
GROUP BY 1
) t2 ON t1.calldate = t2.calldate
Виртуальный выделенный сервер (VDS) становится отличным выбором
Есть activity c webview, в котором открывается банковская страница оплатыДля подтверждения оплаты банк высылает sms код
Есть форма и таблица с даннымиОткрываю нужный объект для редактирование в первый раз, естественно всё нормально