简体   繁体   English

MySQL计算两个子查询之间的百分比

[英]mysql calculate percentage between two sub queries

I am trying to work out the percentage of a number of students who meet certain criteria. 我正在尝试计算出符合某些条件的学生百分比。 I have 3 separate tables that I need to get data from, and then I need to get the total from one table (student) as the total of students. 我有3个单独的表,我需要从中获取数据,然后我需要从一个表(学生)中获取总数作为学生总数。 Then I need to use this total, to divide the COUNT of the no of students in the 2nd query. 然后,我需要使用此总数,以在第二个查询中除以COUNT个学生人数。

So basically I am trying to get a count of ALL the students that are in the DB first. 因此,基本上,我试图首先统计数据库中所有学生的人数。 Then count the no of students that appear in my main query (the one returning the data). 然后计算出现在我的主要查询中的学生人数(返回数据的学生人数)。 Then I need to perform the calculation that will take the noOfStudents (2) and divide by the main total (24) (no of students in DB) then *100 to give me the percentage of students who have met the criteria in the main query. 然后,我需要执行计算,将noofStudents(2)除以主要总数(24)(数据库中的学生人数),然后乘以* 100,得出满足主要查询条件的学生的百分比。

This is what I have so far: 这是我到目前为止的内容:

SELECT * FROM (
(
SELECT s.firstname, s.lastname, s.RegistrationDate, s.Email, d.ReviewDate,(r.description) AS "Viva"  , COUNT(*) AS "No of Students"
FROM student s
INNER JOIN dates d 
ON s.id=d.student_identifier 
INNER JOIN reviews r
ON d.review_Identifier=r.id 
WHERE r.description = "Viva Date"
GROUP BY s.student_identifier
ORDER BY s.student_identifier)
) AS Completed
WHERE Completed.ReviewDate BETWEEN '2012-01-01' AND '2014-12-01'
;

I need to output the fields following the second SELECT and this data in turn will be displayed via PHP/HTML code on a page (the BETWEEN dates will be sent via '%s'). 我需要在第二个SELECT之后输出字段,然后该数据将通过PHP / HTML代码显示在页面上(BETWEEN日期将通过'%s'发送)。

I wondered if I should be using 2 separate queries and then getting the value (24) from the first query to perform the calculation in the second query, but I have not been able to work out how to save as 2 separate queries and then reference the first query. 我想知道是否应该使用2个单独的查询,然后从第一个查询中获取值(24)以在第二个查询中执行计算,但是我无法弄清楚如何另存为2个单独的查询,然后进行引用第一个查询。

I am also not sure if it is possible to display an overall % total at the same time as outputting the individual rows that meet the criteria? 我也不确定是否可以在输出符合条件的单个行的同时显示总计百分比?

I am trying to teach myself SQL, so I apologise if I have made any glaring mistakes/assumptions in any of the above, and would appreciate any advice that's out there. 我正在尝试自学SQL,因此,如果我在上述任何方面犯了任何明显的错误/假设,我深表歉意,并且感谢您提出的任何建议。

Thank you. 谢谢。

Could you do this? 你能做到吗?

SELECT COUNT(*) as TotalPopulation, 
COUNT(d.student_identifier='student') as TotalStudents, 
COUNT(d.student_identifier='student')/ count(*) *100 as Percentage of students  
from students s 
inner join dates d
on s.id = d.student_identifier
inner join reviews r
on r.id = d.review_Identifier
WHERE d.ReviewDate BETWEEN '2012-01-01' AND '2014-12-01' and r.description = 'Viva Date';

You do not need first name last name if you are just looking for counts, necessarily. 如果您只是在寻找数字,则不需要姓氏。 This get's the count(*) of table, then whatever flag you use to identify a student in the second count(), you just had it grouped by before, which could give you wrong results considering there's much else in your select before aggregation. 这是表的count(*),然后使用第二个count()中用来标识学生的任何标志,您只是将其分组之前,这可能会给您带来错误的结果,因为考虑到聚合之前选择的内容还有很多。

You could also try: 您也可以尝试:

SELECT d.student_identifier, s.firstname, s.lastname, 
s.RegistrationDate, s.Email, d.ReviewDate,(r.description) AS "Viva" 
FROM student s
INNER JOIN dates d 
ON s.id=d.student_identifier 
INNER JOIN reviews r
ON d.review_Identifier=r.id 
WHERE r.description = "Viva Date" and d.ReviewDate BETWEEN '2012-01-01' AND '2014-12-01'
ORDER BY s.student_identifier

Now, if you want to return a list, that's the second one, if you want to return a count, you would use the first query and adjust to your student_identifier. 现在,如果要返回列表,则为第二个列表,如果要返回计数,则可以使用第一个查询并调整为student_identifier。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM