0

我有当前的数据库设置:

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

希望这是有道理的!?

谢谢荷马。

4

2 回答 2

1

SELECT dept,
       COUNT (dept) AS total,
       COUNT (completion_status) AS 来自结果的响应
WHERE
completion_status IN ('Started', 'Completed')
GROUP BY dept
ORDER BY dep 

于 2011-05-01T12:22:57.877 回答
0
select 
department,
count(department) as total,
sum(if(completion_status  IN ('Started', 'Completed'),1,0)) as responses
from table
group by department

这是一个更标准的版本

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
于 2011-05-01T12:24:44.610 回答