Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2005
    Posts
    266

    Unanswered: how can I get the percentage ?

    I am having

    SELECT DISTINCT Name,
    (
    ( count(id_Name) * 100 ) / (SELECT count(id_Name) AS tt FROM dbo.Name)
    )
    as Percentage
    FROM dbo.Name
    GROUP BY Name


    but I get only integers : 1,0,4,12
    I need : 1.63, 0.4, 4.05, 12.78

    how can I do it ?
    is there a better way to calculate a percentage ?

    thank you

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Code:
    SELECT name.Name
    , Percentage = count(*) / totty.tot * 100
    FROM dbo.Name
    CROSS JOIN
    (SELECT tot  = COUNT(*) * 1.0 FROM dbo.Name) AS totty
    GROUP BY Name
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Dec 2005
    Posts
    266
    thank you but I am getting
    Column 'totty.tot' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    if I do

    SELECT name.Name
    , Percentage = (count(*) / ((SELECT tot = COUNT(*) * 1.0 ) FROM dbo.Name)) * 100)
    FROM dbo.Name
    GROUP BY Name


    it works but I get 1.88679245283000 , how can I get 1.89 ? (2 numbers after the dot )


    thanks
    Last edited by anselme; 03-18-08 at 08:07.

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Whoops

    Code:
    SELECT [name].[name]
            , Percentage = count(*) / totty.tot * 100
    FROM    dbo.[name]
    CROSS JOIN
            (SELECT tot  = COUNT(*) * 1.0 
            FROM    dbo.[name]) AS totty
    GROUP BY [name]
            , totty.tot
    Round off in your front end application. Otherwise, look up ROUND().
    Testimonial:
    pootle flump
    ur codings are working excelent.

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    [offtopic]NZDF bronze medal for totty.tot[/offtopic]
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  6. #6
    Join Date
    Dec 2005
    Posts
    266
    thank you :-)

Posting Permissions

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