sql - How to combine different queries results into a single query result table -
sql - How to combine different queries results into a single query result table -
here thing.i trying find query can include there 3 results. i know how query 1 of them each.
questions: each survey has had @ to the lowest degree 200 members participate, provide next information: 1)survey id , survey description 2)number of members started survey 3) number of members finished.
query survey id , survey description have @ to the lowest degree 200 participations
1)
select survey_id, survey_desc, count(tbl_survey.member_id) totalnumber tbl_survey,tbl_member_participation tbl-survey.member_id = tbl_member_participation.member_id grouping survey_id, survey_desc having totalnumber >= 200
2) query number of members started not finished.
select count(survey_id) tbl_survey, survey_id exists ( select survey_id, survey_desc, count(tbl_survey.member_id) totalnumber tbl_survey,tbl_member_participation tbl-survey.member_id = tbl_member_participation.member_id grouping survey_id, survey_desc having totalnumber >= 200) , finishi_date **null** grouping survey_id
3) query number of members finished.
select count(survey_id) tbl_survey, survey_id exists ( select survey_id, survey_desc, count(tbl_survey.member_id) totalnumber tbl_survey,tbl_member_participation tbl-survey.member_id = tbl_member_participation.member_id grouping survey_id, survey_desc having totalnumber >= 200) , finishi_date ***not null*** grouping survey_id
so want way can combine these 3 results survey id, survey desc, start people, finish people. in 1 query table.
first of all, should queries right.
1) first query okay, should not utilize out-dated comma-separated bring together syntax. utilize explicit joins instead (that introduced in sql standard 20 years ago, btw).
select s.survey_id, s.survey_desc, count(*) totalnumber tbl_survey s bring together tbl_member_participation mp on s.member_id = mp.member_id grouping s.survey_id, s.survey_desc having count(*) >= 200;
2,3) sec , 3rd queries syntactically broken. kind of mixing exists , in. finishi_date fellow member of tbl_member_participation, must bring together table again. here query 2 corrected:
select s.survey_id, count(*) tbl_survey s bring together tbl_member_participation mp on s.member_id = mp.member_id survey_id in ( select s.survey_id tbl_survey s bring together tbl_member_participation mp on s.member_id = mp.member_id grouping s.survey_id having count(*) >= 200 ) , mp.finishi_date null grouping s.survey_id;
in order combine three, don't have utilize exists or in. info needed available in query 1 already. look, how modify query 1 much simpler query 2:
select s.survey_id, sum(case when mp.finishi_date null 1 else 0 end) count_unfinished tbl_survey s bring together tbl_member_participation mp on s.member_id = mp.member_id grouping s.survey_id having count(*) >= 200;
having said this, final query this:
select s.survey_id, s.survey_desc, sum(case when mp.finishi_date null 1 else 0 end) count_unfinished, sum(case when mp.finishi_date not null 1 else 0 end) count_finished, count(*) totalnumber tbl_survey s bring together tbl_member_participation mp on s.member_id = mp.member_id grouping s.survey_id, s.survey_desc having count(*) >= 200;
sql operation
Comments
Post a Comment