常规错误:1111无效使用组功能

时间:2015-09-02 13:03:41

标签: mysql

我有以下的sql查询,但是当我尝试运行查询时,它会抛出一个没有1111的错误,请一些身体帮助。

select count( distinct id_student) count, 
CASE 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') < CURDATE()) THEN 'Subscription Expired' 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') >= CURDATE() && sum(completed_test_count) = sum(total_test_count) ) THEN 'Test Completed' 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') >= CURDATE() && sum(completed_test_count) < sum(total_test_count) ) THEN 'Active' 
END status 
from `order` 
inner join `student` on `id_student` = `order`.`student_id_student` 
inner join `order_has_test` on `order_id_order` = `order`.`id_order` 
inner join `subscription_plan` on `id_subscription_plan` = `subscription_plan_id_subscription_plan` 
where `subscription_plan_id_subscription_plan` = 1 
group by `order`.`student_id_student`, 
CASE 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') < CURDATE()) THEN 'Subscription Expired' 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') >= CURDATE() && sum(completed_test_count) = sum(total_test_count) ) THEN 'Test Completed' 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') >= CURDATE() && sum(completed_test_count) < sum(total_test_count) ) THEN 'Active' 
END 

2 个答案:

答案 0 :(得分:0)

group by应该只包含案例

select count(distinct id_student) count, 
CASE 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') < CURDATE()) THEN 'Subscription Expired' 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') >= CURDATE() && sum(completed_test_count) = sum(total_test_count) ) THEN 'Test Completed' 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') >= CURDATE() && sum(completed_test_count) < sum(total_test_count) ) THEN 'Active' 
END status 
from `order` 
inner join `student` on `id_student` = `order`.`student_id_student` 
inner join `order_has_test` on `order_id_order` = `order`.`id_order` 
inner join `subscription_plan` on `id_subscription_plan` = `subscription_plan_id_subscription_plan` 
where `subscription_plan_id_subscription_plan` = 1 
group by
CASE 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') < CURDATE()) THEN 'Subscription Expired' 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') >= CURDATE() && sum(completed_test_count) = sum(total_test_count) ) THEN 'Test Completed' 
    WHEN (date_format(max(expiry_date),'%Y-%m-%d') >= CURDATE() && sum(completed_test_count) < sum(total_test_count) ) THEN 'Active' 
END 

答案 1 :(得分:0)

在研究并尝试了一些测试用例后,我得到了解决方案。

从(选择计数(id_student)计数中选择计数(计数)计数,状态, 案件     WHEN(date_format(max(expiry_date),'%Y-%m-%d')&lt; CURDATE())然后'订阅已过期'     WHEN(date_format(max(expiry_date),'%Y-%m-%d')&gt; = CURDATE()&amp;&amp; sum(completed_test_count)= sum(total_test_count))然后'测试完成'     WHEN(date_format(max(expiry_date),'%Y-%m-%d')&gt; = CURDATE()&amp;&amp; sum(completed_test_count)&lt; sum(total_test_count))THEN'Active' 结束状态 来自order student = id_student上的内部联接orderstudent_id_student order_has_test = order_id_order上的内部联接orderid_order subscription_plan = id_subscription_plan上的内部联接subscription_plan_id_subscription_plan 其中subscription_plan_id_subscription_plan = 1 按orderstudent_id_student)数据分组 按状态分组