我试图通过使用SUM()来获取三列的计数,当我执行查询时,它说“#1111-组功能的无效使用”。我尝试更正了组功能,即使它显示了错误。

我需要2、3、4列的完美计数

SELECT c.name,

    SUM(CASE WHEN COUNT(distinct cmc.id) = COUNT(distinct ccc.id) THEN 1 ELSE 0 END) AS "COND1",
    SUM(CASE WHEN COUNT(distinct cmc.id) > 0 AND COUNT(distinct cmc.id) != COUNT(distinct ccc.id) THEN 2 ELSE 0 END) AS "COND2",
    SUM(CASE WHEN COUNT(distinct cmc.id) = 0 THEN 3 ELSE 0 END) AS "COND3"

    FROM
    lesson_modules_completion cmc
    INNER JOIN lesson_modules cm ON cmc.lessonmoduleid = cm.id
    INNER JOIN lesson_completion_settings ccc ON cm.lesson = ccc.lesson
    INNER JOIN lesson c ON cm.lesson = c.id
    INNER JOIN user u ON u.id = cmc.userid
    WHERE ccc.criteriatype=4
    GROUP BY c.name


我需要每个课程的每个用户都有完成模块,进程内模块和未启动模块的数量,其中通过从表CM中获取课程来从表CC中获取用户ID的数量,并从每个课程中获取用户已完成的模块数量。

(一门课程可以有多个模块,而一门课程可以有尝试所有模块的用户数量,很少有模块或者根本没有尝试过)。

因此,在一门课程中,我需要大量的用户-已经完成了许多模块。 (3个逻辑)

(COND1,即Completed.Users):如果尝试的模块数等于表CMS中的modinstance数(例如:每个课程用户尝试的模块数= 9,则no.modinstance =9。因为7不等于至9,它们完成了。)

(COND2,即Inprocess.Users):尝试的模块数应大于0,但不等于[每门课程的count(modinstance)](例如:每门课程的用户尝试的模块数= 7,no.modinstance = 9.因为7不等于9,所以它们正在处理中。)

(COND2,即Nostart.Users):尝试的模块数应等于0,(例如:每门课程的用户尝试的模块数=0。它们没有启动)。

您可以参考我的查询,一切都很好,但是我需要计数COND1,COND2,COND3

我需要输出:

 lesson        COND1              COND2                  COND3
 5              65                 32                       6
 6              40                 12                       15
 8              43                 56                       0
 9               0                 7                        9

最佳答案

select lesson,sum(COND1) as COND1, sum(COND2) as COND2,sum(COND3) as COND3
from
(
SELECT c.name,
       ccc.lesson,
    CASE WHEN COUNT(distinct cmc.id) = COUNT(distinct ccc.id) THEN 1 ELSE 0 END AS "COND1",
    CASE WHEN COUNT(distinct cmc.id) > 0 AND COUNT(distinct cmc.id) != COUNT(distinct ccc.id) THEN 2 ELSE 0 END AS "COND2",
    CASE WHEN COUNT(distinct cmc.id) = 0 THEN 3 ELSE 0 END AS "COND3"

    FROM
    lesson_modules_completion cmc
    INNER JOIN lesson_modules cm ON cmc.lessonmoduleid = cm.id
    INNER JOIN lesson_completion_settings ccc ON cm.lesson = ccc.lesson
    INNER JOIN lesson c ON cm.lesson = c.id
    INNER JOIN user u ON u.id = cmc.userid
    WHERE ccc.criteriatype=4
    GROUP BY c.name,ccc.lesson
) t
group by lesson

10-08 16:15