How to Come up with a percentage of a column of True/False Data

  • I know this is an easy thing but I'm just learning and am self taught.

    I have a table of data that contains a lot of True/False records. What is a good way to come up with what percentage of a column is answered True?

    This result will then be displayed in a web page most likely in a Gridview using visual web developer.

    Thanks in advance

    Mike

  • SELECT QuestionID,

    Yes,

    Yes / Answers,

    No,

    No / Answers,

    Answers

    FROM (

    SELECT QuestionID,

    SUM (CASE WHEN Answer = 1 THEN 100.0 ELSE 0.0 END) AS Yes,

    SUM(CASE WHEN Answer = 0 THEN 100.0 ELSE 0.0 END) AS No,

    COUNT(*) AS Answers

    FROM Questions

    GROUP BY QuestionID

    ) AS d


    N 56°04'39.16"
    E 12°55'05.25"

  • Maybe the caffeine just wore off - but why are you adding up increments of 100?

    ....

    sum(Case when Answer=1 then 100.0 else 0 end),

    ....

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • A possible alternative is

    SELECT QuestionID,

    COUNT(*) AS CountRows,

    (CAST(COUNT(*) AS float) / (SELECT COUNT(*) FROM dbo.Answers)) * 100 AS PercentRows

    FROM dbo.Answers

    GROUP BY

    QuestionID

  • Instead of return 0.45 which in mathematical term is 45%, the algorithm returns 45.0, which most beginners find more appealing.


    N 56°04'39.16"
    E 12°55'05.25"

  • Thank you for your reply and help.

    I get most of it but I'm confused about this section

    Yes,

    Yes / Answers,

    No,

    No / Answers,

    Answers

    Could you (or anyone) elaborate on this part. Or tell me where I could learn about this.

  • Thanks for the help. I do see I have to read up on what CAST means.

    Is there a section on this site or another that can be used to learn about Keywords like that?

  • BOOKS ONLINE is your best friend 🙂


    * Noel

Viewing 8 posts - 1 through 7 (of 7 total)

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