我有一个查询,其中一个课程(C1,C5,C9 ......等)应该显示状态为DONE/NOT DONE/PENDING
的用户的帐户作为结果。请注意,课程可以是DONE/NOT DONE
或PENDING
:
count(u.userid) ll.law_name ll.law_course ll.law_lesson ll.score ll.status
4 Berlyin C1 L1 6.05 Done
8 Berlyin C1 L1 NULL Not Done
4 Elance C9 L6 3.4 Done
7 Elance C9 L6 NULL Not Done
2 Indix C5 L12 5.5 Done
3 Indix C10 L3 NULL Not Done
SELECT count(DISTINCT(u.userid)), ll.law_name, ll.law_course, ll.law_lesson,
CASE WHEN ll.score >=0 THEN Done
WHEN ll.score <0 THEN Not Done
WHEN ll.score !=0 THEN pending END AS "status"
FROM
users u JOIN law_courses ll ON u.id = ll.userid GROUP BY ll.law_lesson
答案 0 :(得分:1)
基本思路是将case
语句放在group by
中。你对这三个群体的逻辑没有意义(或者至少,&#34;未决&#34;群体不存在)。但这可能会让你走上正轨:
SELECT count(*), ll.law_name, ll.law_course, ll.law_lesson,
(CASE WHEN ll.score >= 0 THEN 'Done'
WHEN ll.score < 0 THEN 'Not Done'
ELSE 'Pending'
END) as status
FROM users u JOIN
law_courses ll
ON u.id = ll.userid
GROUP BY ll.law_name, ll.law_course, ll.law_lesson,
(CASE WHEN ll.score >= 0 THEN 'Done'
WHEN ll.score < 0 THEN 'Not Done'
ELSE 'Pending'
END);