SELECT
uid,
COUNT(incomplete) incomplete_cnt,
COUNT(complete) complete_cnt,
GROUP_CONCAT(
DISTINCT CONCAT_WS(':', DATE(start_time), tag) SEPARATOR ';'
) detail
FROM
(
SELECT
er.uid uid,
ei.tag tag,
er.start_time start_time,
IF(submit_time IS NULL, 1, NULL) incomplete,
IF(submit_time IS NULL, NULL, 1) complete
FROM
exam_record er
INNER JOIN examination_info ei ON er.exam_id = ei.exam_id
WHERE
YEAR(start_time) = 2021
) tb
GROUP BY
uid
HAVING
complete_cnt >= 1
AND incomplete_cnt BETWEEN 2 AND 4
ORDER BY
incomplete_cnt DESC;