# Thread: how can I get the percentage ?

1. Registered User
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. King of Understatement
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```

3. Registered User
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 07:07.

4. King of Understatement
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().

5. SQL Consultant
Join Date
Apr 2002
Location
Posts
20,002
[offtopic]NZDF bronze medal for totty.tot[/offtopic]

6. Registered User
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
•