У меня есть запрос, в котором я выбираю все sendIntakes и группирую их с идентификатором и годом.
Столбец sendResponse - если они ответили, 0 - Нет, 1 - Да.
SELECT officiantId, COUNT(*) as ct, sendResponse,
Year(created_at) as yr
FROM sendIntakes
Where sendResponse is not null
GROUP BY officiantId, sendResponse, YEAR(created_at)
ORDER BY officiantId, yr
DESC;
Я сделал свой счет, хотя он делает новую строку, если число равно 0/1
officiant id - 404 | ct - 2 | sendResponse 1 | yr 2017
officiant id - 404 | ct - 1 | sendResponse 0 | yr 2017
officiant id - 547 | ct - 1 | sendResponse 1 | yr 2017
Есть ли способ избавиться от sendResponse и использовать его как
officiant id - 404 | ct - 2 | Yes - 1 | No - 1 | yr 2017
Вы можете подсчитать каждое событие да и нет (1 или 0), избавившись от sendResponse в своей группе GROUP BY
SELECT officiantId, COUNT(*) as ct,
SUM(CASE WHEN sendResponse = 1 THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN sendResponse = 0 THEN 1 ELSE 0 END) as No,
Year(created_at) as yr
FROM sendIntakes
Where sendResponse is not null
GROUP BY officiantId, YEAR(created_at)
ORDER BY officiantId, yr
DESC;
END CASE
. Вы можете попробовать посмотреть, есть ли проблема: MySQL использует Sum и Case