简体   繁体   中英

Mysql - How do I find the ratio of male to female who have downloaded a program

I am sure most people will be annoyed at my question, but I am a newbie student and I am trying to work out this query - please help. Essentially I want to know the ratio of male/female who have downloaded each program. I am guessing I need to join the tables I have pasted below, but I keep getting errors

User Table (there are many attributes in the User table but I am just focusing on the items I need from that table)

|User_ID|Gender|

Library Table

|User_ID|Program_ID|

Program Table (I have included this but I do not think I need to use this table UNLESS I want to also include the program name in my result)

|Program_ID|Developer_ID|Program_Name|

This is the first query I thought to run - being percentage of male/female.

select User_id, Program_id

100*sum(case when gender = male then 1 else end 0)/count male_perc

100*sum(case when gender = female then 1 else end 0)/count female_perc

From Library

Group by Program_ID, Program_name

Order by count (*) desc

User Table

Library Table

Program Table

Welcome, and correct, you need some help. Good start, but need help. As a newbie, I will try to explain by starting with the FROM clause. How are you getting the things you want. In this case you need the connection between the user and the library of stuff downloaded. Of that, what is the actual "JOIN" condition. This builds out your FROM statement. You will see many queries that use "alias" names to prevent long-hand retyping of tables over and over. You should get used to that now. It also prevents ambiguous which column is associated with which table.

FROM
   Library l
      JOIN User u
        on l.user_id = u.user_id

At this point, since you dont care about the specific name (yet), you dont need the program table, but your thought of how is good. I will do the counts separately and pull the % and name in at the end.

select
        l.program_id,
        count(*) TotalDownloads,
        sum( case when u.gender = 'male' then 1 else 0 end ) DownloadByMale,
        sum( case when u.gender = 'female' then 1 else 0 end ) DownloadByFemale
    FROM
        Library l
            JOIN User u
                on l.user_id = u.user_id
    GROUP BY
        l.program_id

So, this gives all the raw counts pre respective library program and actual counts per gender category male/female. Now, this can be re-joined again to get the program name and compute the respective percentages. Since the above query is the basis for the next, it can be "wrapped" into the next and given its own alias. I will assign the alias "preAgg" since it is a pre-aggregation of the stuff, so the final columns from that query will have the "preAgg" alias name in the outer query.

select
        p.program_name,
        preAgg.TotalDownloads,
        100 * ( preAgg.DownloadByMale / preAgg.TotalDownloads ) PcntByMale,
        100 * ( preAgg.DownloadByFemale / preAgg.TotalDownloads ) PcntByFemale
    from
        (select
                l.program_id,
                count(*) TotalDownloads,
                sum( case when u.gender = 'male' then 1 else 0 end ) DownloadByMale,
                sum( case when u.gender = 'female' then 1 else 0 end ) DownloadByFemale
            FROM
                Library l
                    JOIN User u
                        on l.user_id = u.user_id
            GROUP BY
                l.program_id ) preAgg
            JOIN program p
                on preAgg.program_id = p.program_id
    order by
        preAgg.TotalDownloads desc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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