how to combine results of these two queries?

  • i have these tables :

    Students

    Scholarships

    Students_Scholarships_junction

    Defaulters

    Defaulters_Scholarships_junction

    And these are my two queries:

    select scholarship_category , COUNT(s.scholarship_id) as [No of applicants]

    from Students s

    join Students_Scholarships_junction j on j.student_pk = s.student_pk

    join Scholarships on j.scholarship_id = Scholarships.scholarship_id

    where s.student_fullname is not null

    group by scholarship_category

    select scholarship_category ,COUNT (distinct student_fullname) as [No of duplicates]

    from Defaulters

    join Defaulters_Scholarships_junction j on j.Dstudent_pk = Defaulters .Dstudent_pk

    join Scholarships on j.scholarship_id = Scholarships .scholarship_id

    group by scholarship_category

    i wish to combine these two queries such that the result will consist of three column

    Scholarship category , No of applicants , No of duplicates

    i have tried union and join but not getting it right

  • Something like this?

    select

    sc.scholarship_category

    ,COUNT(s.scholarship_id) as [No of applicants]

    ,COUNT (distinct d.student_fullname)

    from Students s

    join Students_Scholarships_junction ss on ss.student_pk = s.student_pk

    join Scholarships sc on ss.scholarship_id = sc.scholarship_id

    join Defaulters d on s.student_pk = d.Dstudent_pk

    where s.student_fullname is not null

    group by sc.scholarship_category

    John

  • its showing correct value for 'no of duplicates' but not for 'no of applicants'. in fact its repeating the same value in both the fields.

  • More like this:

    WITH Query1 AS(

    select scholarship_category , COUNT(s.scholarship_id) as [No of applicants]

    from Students s

    join Students_Scholarships_junction j on j.student_pk = s.student_pk

    join Scholarships on j.scholarship_id = Scholarships.scholarship_id

    where s.student_fullname is not null

    group by scholarship_category

    ),

    Query2 AS(

    select scholarship_category ,COUNT (distinct student_fullname) as [No of duplicates]

    from Defaulters

    join Defaulters_Scholarships_junction j on j.Dstudent_pk = Defaulters .Dstudent_pk

    join Scholarships on j.scholarship_id = Scholarships .scholarship_id

    group by scholarship_category

    )

    SELECT q1.scholarship_category, q1.[No of applicants], q2.[No of duplicates]

    FROM Query1 q1

    JOIN Query2 q2 ON q1.scholarship_category = q2.scholarship_category;

    You might need to change the inner join to an outer join.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply