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 8099355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:23:04+00:00 2026-06-05T22:23:04+00:00

I have a table where each row has a description field as well as

  • 0

I have a table where each row has a description field as well as a boolean value. I’m trying to write a query where I can group by each respective description, and see the percentage of times that the boolean was true.

Example table:

    PID    Gender    SeniorCitizen
     1       M             1
     2       M             1
     3       F             0
     4       F             1
     5       M             0

And I want a query that will return this:

    Gender    SeniorPct
      M         .66
      F         .50

I’ve got to the point where I have a query that will calculate the individual percentages for a male or female – but I want to see both results at once

    SELECT Gender, COUNT(*) * 1.0 / 
                   (SELECT COUNT(*) FROM MyTable WHERE Gender='M') 
    FROM MyTable WHERE Gender='M' and SeniorCitizen=1;

I’ve been trying to include a “GROUP BY Gender” statement in my outer SELECT above, but I can’t seem to figure out how to tweak the inner SELECT to get the correct results after tweaking the outer SELECT as such.

  • 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-06-05T22:23:05+00:00Added an answer on June 5, 2026 at 10:23 pm

    (I tested this under MySQL, please check if the same idea can be applied to the SQLite.)

    To find the number of seniors (per gender), we can treat the bits as numbers and simply sum them up:

    SELECT
        Gender,
        SUM(SeniorCitizen) Seniors
    FROM MyTable
    GROUP BY Gender
    
    GENDER  SENIORS
    M       2
    F       1
    

    Based on that, we can easily calculate percentages:

    SELECT
        Gender,
        SUM(SeniorCitizen) / COUNT(*) * 100 SeniorsPct
    FROM MyTable
    GROUP BY Gender
    
    GENDER  SENIORSPCT
    M       66.6667
    F       50
    

    You can play with it in this SQL Fiddle.


    UPDATE: Very similar idea works under SQLite as well. Please take a look at another SQL Fiddle.

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

Sidebar

Related Questions

I have a table where each row has some data and the user can
I have a table where each row has a timestamp. Is there a query
I have a table where each row has 13 TD elements. I want to
I have a table column where each row has one of three states, and
I have a DB table in which each row has a randomly generated primary
I have a table with 3 rows. Each row has 3 td. The first
I have this kind of table, it has many rows and each row has
I have a tableview with custom UITableViewCells, each row in the table has a
I have a table containing a large number of rows. Each row has 2
I have a table with a list of businesses. Each row has details such

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.