Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2006
    Posts
    3

    Unanswered: help needed with sql query. percentage of the females.

    Dear All
    I have following table

    id|name|female

    1|John|No
    2|Brian|No
    3|Tanya|Yes

    How can I get percentage of females in this table?

    Thanks a lot!

    Svirid

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Code:
    SELECT Sum(CASE WHEN 'Yes' = female THEN 1e0 END) / Count(*)
       FROM myTable
    -PatP

  3. #3
    Join Date
    Nov 2006
    Location
    UK
    Posts
    46
    This should be faster on big number of records:

    Code:
    select FemalesPercentage = 100.0 * sum(case when female = 'Yes' then NumberOf else 0.0 end) / sum(NumberOf)
    from (select female,
                 NumberOf = count(1)
          from SviridsTable
          group by female) as x
    Regards
    Kris Zywczyk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •