简体   繁体   English

将两个查询合并为一个查询

[英]Combining two queries into one query

Query 1 查询1

select test_id, count (student_id)*100/ 
  (select count(distinct student_id)from test_taken)as "pass rate"
from test_taken
where result>50
group by test_id;

Query 2 查询2

 select test_id, count (student_id)*100/ 
    (select count(distinct student_id)from test_taken)as "fail rate"
 from test_taken
 where result<50
 group by test_id;

I have the following Table: 我有下表:

test_taken
Columns:test_id,student_id,result

I am looking to get the percentage pass rate and fail rate where a pass is result >50% and fail is result <50%. 我正在寻找通过率>失败率和失败率的百分比,结果是通过率> 50%,失败是结果<50%。

I have the pass rate and fail rate as 2 separate queries but I want them combined into one query. 我有2个单独的查询的通过率和失败率,但我希望将它们组合成一个查询。

SELECT test_id, 
       sum(case when result > 50 then 1 else 0 end) * 100 / (SELECT COUNT(DISTINCT student_id) 
                                   FROM   test_taken) AS "pass rate",
       sum(case when result < 50 then 1 else 0 end) * 100 / (SELECT COUNT(DISTINCT student_id) 
                                   FROM   test_taken) AS "fail rate" 
FROM   test_taken 
GROUP  BY test_id; 

If the result of the two queries is equal in terms of column numbers and column types, you can use UNION to get one table result : 如果两个查询的结果在列号和列类型方面相等,则可以使用UNION获得一个表结果:

SELECT test_id, 
       COUNT (student_id) * 100 / (SELECT COUNT(DISTINCT student_id) 
                               FROM   test_taken)AS rate 
FROM   test_taken 
WHERE  result > 50 
GROUP  BY test_id; 

UNION

SELECT test_id, 
       COUNT (student_id) * 100 / (SELECT COUNT(DISTINCT student_id) 
                               FROM   test_taken)AS rate 
FROM   test_taken 
WHERE  result < 50 
GROUP  BY test_id;

The OPs solution does not produce a correct result (at least not the one specified in the requirements) so expanding on that with UNION or other methods is NOT a valid answer. OP解决方案无法产生正确的结果(至少不是要求中指定的结果),因此使用UNION或其他方法扩展该解决方案不是有效的答案。 He seems to be calculating the percentages on the entire number of students instead of just the ones that took a specific test. 他似乎是在计算学生总数的百分比,而不是仅仅计算参加特定考试的百分比。

A query which would produce the correct result for each test is the following: 下面的查询将为每个测试生成正确的结果:

select Q1.test_id, 
   Q1.students_passed * 100 / Q1.total_students || '%' as pass_rate,
   Q1.students_failed * 100 / Q1.total_students || '%' as fail_rate
from       
(SELECT test_id, 
   sum(case when result > 50 then 1 else 0 end) students_passed,
   sum(case when result < 50 then 1 else 0 end) students_failed,
   count(distinct student_id) total_students
FROM   test_taken 
GROUP  BY test_id) Q1;

We first count how many students have passed and failed using the SUM function. 我们首先使用SUM函数计算通过和未通过的学生人数。 We add one to the sum when our coditions are met, ie result > 50 or result < 50, else we add zero. 当满足条件时,我们将总和加一,即结果> 50或结果<50,否则我们将零加。

We also need to count the total nr. 我们还需要计算总数nr。 of students that have taken a test, so we can do that easily with count(distinct student_id) and grouping by test_id. 的学生参加了考试,因此我们可以轻松地通过count(distinct student_id)和按test_id分组来做到这一点。

Finally we wrap this query in the outer one, in which we divide the the nr. 最后,我们将此查询包装在外部查询中,在其中将nr除以。 of students passed and failed to the total nr of students for each a given test_id. 的学生通过了考试,但未通过每个给定test_id的学生总数nr。

For the input table: 对于输入表:

1  | 1 | 51
1  | 2 | 30
2  | 3 | 60
2  | 4 | 22
3  | 2 | 66

it produces the output: 它产生输出:

1  | 50%  | 50%
2  | 50%  | 50%
3  | 100% | 0%

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

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