Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 469723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:48:47+00:00 2026-05-12T23:48:47+00:00

given a table that for the following commands: select sex, count(*) from my_table group

  • 0

given a table that for the following commands:

select sex, count(*) from my_table group by sex;
select sex, employed, count(*) from my_table group by sex, employed;

gives:

  sex  | count 
-------+------
male   | 1960 
female | 1801

and:

 sex     | employed | count 
---------+----------+-------
 male    | f        |  1523 
 male    | t        |   437 
 female  | f        |  1491 
 female  | t        |   310 

I’m having a difficulty writing a query that will calculate percentage of employed within each sex group. So the output should look like this:

 sex     | employed | count  | percent
---------+----------+--------+-----------
 male    | f        |  1523  | 77.7% (1523/1960)
 male    | t        |   437  | 22.3% (437/1960)
 female  | f        |  1491  | 82.8% (1491/1801)
 female  | t        |   310  | 17.2% (310/1801)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:48:47+00:00Added an answer on May 12, 2026 at 11:48 pm

    You can do it with a sub-select and a join:

    SELECT t1.sex, employed, count(*) AS `count`, count(*) / t2.total AS percent
      FROM my_table AS t1
      JOIN (
        SELECT sex, count(*) AS total 
          FROM my_table
          GROUP BY sex
      ) AS t2
      ON t1.sex = t2.sex
      GROUP BY t1.sex, employed;
    

    I can’t think of other approaches off the top of my head.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: A table named Table1 that has the following columns: ID ColumnA ColumnB Typing
Given a table or a temp table, I'd like to run a procedure that
Given an HTML page that has a complex table-based layout and many tags that
Given an ASP.NET MVC view that generates a table of entries using a for
I have a <table/> in a ViewUserControl that I have given the tag a
Problem Given the following two tables, I'd like to select all Ids for Posts
Assume I have a table that has the following structure: ================= | Id |
Given a table such as the following: <tbody> <%foreach (var book in Model.Books) {
Given the following table structure, how can I use a Linq query to return
Given the following table: Sequence Tag ----- ---- 1 a 2 a 3 a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.