我有以下的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
答案 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
上的内部联接order
。student_id_student
order_has_test
= order_id_order
上的内部联接order
。id_order
subscription_plan
= id_subscription_plan
上的内部联接subscription_plan_id_subscription_plan
其中subscription_plan_id_subscription_plan
= 1
按order
。student_id_student
)数据分组
按状态分组