基于mysql中单个表中的条件的行数和行数的百分比

时间:2017-04-28 08:59:27

标签: mysql

我有四个表格,结构如下。

Table 1:
Project - have unique project names (prj_name)

Table 2:
my_records - have the following fields:
record_id,prj_name,my_dept,record_submit_date,record_state

Table 3:
record_states have multiple states where 'Completed' is one.

Table 4:custom_dept_list
dept_name

我需要获取my_project分组的(记录状态为已完成)和(总记录)的百分比,其中custom_dept_list和record_submit_date中的my_dept大于“某个日期”

我尝试了以下内容:

Query:
    select prj_name,count(record_id) as total,((select count(record_id) from  
my_records where record_state='Completed')/(count(record_id)))*100 as 
percent  from my_records,custom_dept_list where record_state='Completed' 
and   record_submit_date >= ( CURDATE() - INTERVAL 15 DAY ) and  
my_dept=dept_name group by prj_name order by percent desc; 

Total records for project A = 50
Total records for project A with record_state='Completed' = 30
Ratio is not coming - (30/50)*100 = 60
It is giving some very big value.

以下是来自my_records的数据,我删除了record_submit日期以简化:

|1|prj1|dept1|Completed
|2|prj1|dept1|XYZ
|3|prj1|dept1|Completed
|4|prj1|dept2|XYZ
|5|prj1|dept2|Completed
|6|prj1|dept1|XYZ
|7|prj1|dept1|XYZ
|8|prj1|dept1|XYZ
|9|prj1|dept2|XYZ
|10|prj1|dept2|XYZ
|11|prj1|dept2|Completed
|12|prj1|dept2|Completed
|13|prj1|dept2|Completed
|14|prj1|dept3|XYZ
|15|prj1|dept4|Completed
|16|prj1|dept4|XYZ
|17|prj1|dept5|Completed
|18|prj1|dept6|XYZ
|19|prj1|dept7|XYZ
|20|prj1|dept8|XYZ
|21|prj1|dept10|XYZ
|22|prj1|dept2|XYZ
|23|prj1|dept2|Completed
|24|prj1|dept2|Completed
|25|prj1|dept2|Completed

来自Custom_dept_List的数据:

dept_name
dept1
dept3
dept4
dept5
dept6
dept8
dept10

我尝试了以下查询:

查询1

select count(record_id) as count,prj_name from my_records,custom_dept_list where my_dept=dept_name group by prj_name order by count desc;

Ouput -- 13

查询2

select count(record_id) as count,prj_name from my_records,custom_dept_list where my_dept=dept_name and record_state='Completed' group by prj_name order by count desc;

Output -- 4

查询3

select  prj_name,count(record_id) as total,count(case when record_state='Completed' then record_id end) /count(record_id) *100 as percent from my_records join custom_dept_list on my_dept = dept_name where   record_state = 'Completed' group by prj_name order by percent desc;

输出:

prj_name total percent
prj1       4    100.0000

2 个答案:

答案 0 :(得分:2)

首先,请在join子句中使用适当的from代替多个表。

然后,您不需要内部查询来获取具有特定record_state的计数,您可以在case内使用count

select  prj_name,
        count(record_id) as total,
        count(case when record_state='Completed' then record_id end) /
        count(record_id) * 100 as percent
from    my_records
join    custom_dept_list
on      my_dept = dept_name
where   record_submit_date >= ( CURDATE() - INTERVAL 15 DAY )
group by prj_name
order by percent desc;

您的问题可能是由内部查询引起的,该内部查询不是计算每个项目的已完成记录,而是计算所有已完成的记录。

答案 1 :(得分:1)

您不需要此record_state = 'Completed'条件,因此您只能获得完整记录的完整记录。所以试试没有它。

select  prj_name,
        count(record_id) as total,
        count(case when record_state='Completed' then record_id end) /
        count(record_id) * 100 as percent
from    my_records
join    custom_dept_list
on      my_dept = dept_name
where   record_submit_date >= ( CURDATE() - INTERVAL 15 DAY )
group by prj_name