MySQL查询问题
我有当前的数据库设置:MySQL查询问题,mysql,count,where-in,Mysql,Count,Where In,我有当前的数据库设置: Forename | Surname | Department | Completion_Status Tom Smith Sales Started Bob Jones Sales Completed Alison Baines Sales Not Started Arthur Smith Marketing Started Claire S
Forename | Surname | Department | Completion_Status
Tom Smith Sales Started
Bob Jones Sales Completed
Alison Baines Sales Not Started
Arthur Smith Marketing Started
Claire Staines Marketing Completed
我能够在没有任何问题的情况下返回总行数,以及完成状态为Started或Completed但不在单个语句中的总行数-但是我希望在单个语句中执行的操作是:
COUNT for Department As Total AND the COUNT for Department As Responses WHERE Completion_Status IN ('Started', 'Completed')
它看起来像这样:
Department | Total | Responses
Sales 3 2
Marketing 2 2
希望这有意义
谢谢荷马。
选择部门,
阿斯托尔伯爵(部门),
计数(完成状态)作为响应
FROMtresults
其中完成状态为('Started','Completed')
GROUPBYdept
ORDERBYdep 这是一个更标准的版本
select
department,
count(department) as total,
sum(case when completion_status in ('Started', 'Completed') then 1 else 0 end ) as responses
from table
group by department
您忘记在选择列表中添加部门。根据您的建议-选择部门,将(部门)计数为总计,将(完成状态)计数为TreResults中的响应,其中完成状态在('Started','Completed')组中,按部门排序,按部门排序-这将返回SQL错误-有建议吗?您好,重新检查-为所有项返回值1-这是不正确的。选择dept,COUNT(dept)作为总计,COUNT(completion_status)作为TreResults中的响应,其中completion_status='Started'或completion_status='Completed'按部门分组,按depOk下单,这似乎很接近-返回正确的总数,但两个响应都是1,这是不正确的。任何建议?重新检查-返回正确的总计值,但def为响应返回bck 1-这不正确:(
select
department,
count(department) as total,
sum(case when completion_status in ('Started', 'Completed') then 1 else 0 end ) as responses
from table
group by department