Sunday, 15 August 2010

sql - Combining two queries into one query -



sql - Combining two queries into one query -

query 1

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

query 2

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

i have next table:

test_taken columns:test_id,student_id,result

i looking percentage pass rate , fail rate pass result >50% , fail result <50%.

i have pass rate , fail rate 2 separate queries want them combined 1 query.

if result of 2 queries equal in terms of column numbers , column types, can utilize union 1 table result :

select test_id, count (student_id) * 100 / (select count(distinct student_id) test_taken)as rate test_taken result > 50 grouping test_id; union select test_id, count (student_id) * 100 / (select count(distinct student_id) test_taken)as rate test_taken result < 50 grouping test_id;

sql

No comments:

Post a Comment