一个月后对数据进行分组-统计信息

时间:2019-06-18 22:53:28

标签: mysql

我有一个问题,即我想显示每个月的某种统计数据:

|--------------------|----- |-------|
| passed | no_passed | none | month |
|--------------------|------|-------|
| 12     | 34        | 7    | 4     |
|--------------------|------|-------|
| 4      | 17        | 2    | 5     |
|--------|-----------|------|-------|

我在MySQL中的代码:

SELECT COALESCE(q1.passed_questions, 0) passed_questions, 
COALESCE(q2.no_passed_questions, 0) no_passed_questions, COALESCE(q3.none_questions, 0) none_questions, 
MONTH(date_open)

FROM sys_uq uq
        LEFT JOIN (
            SELECT COUNT(*) passed_questions, id_user
            FROM sys_uq
            WHERE status = 1
            GROUP BY id_user, MONTH(date_open)
        ) q1 ON (uq.id_user = q1.id_user)

        LEFT JOIN (
            SELECT COUNT(*) no_passed_questions, id_user
            FROM sys_uq
            WHERE status = 0
            GROUP BY id_user, MONTH(date_open)
        ) q2 ON (uq.id_user = q2.id_user)

        LEFT JOIN (
            SELECT COUNT(*) none_questions, id_user
            FROM sys_uq
            WHERE status < 0
            GROUP BY id_user, MONTH(date_open)
        ) q3 ON (uq.id_user = q3.id_user)
WHERE uq.id_user = 3 AND YEAR(date_open) = 2019
GROUP BY MONTH(uq.date_open)

当我想在一个月后将其分组时出现问题,那么每个月的数据都是相同的-实际上不是。

谢谢您的帮助。

0 个答案:

没有答案