[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# SQL Percentage

Posted on 2012-04-12
Medium Priority
279 Views
Hi Experts,
I use the query below to display the total number of different topics from my table on a pie chart .
Select COUNT(*) as [Count], Topic FROM [myDatabase].[dbo].[myTable] group by Topic

I would like to display the percentage instead of the count. How could I achieve that?
0
Question by:baxtalo
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 13

Accepted Solution

Ashok earned 1000 total points
ID: 37837675
Try

DECLARE @Totals As Int;
set @Totals  = (select Count(*) As Cnt from [myDatabase].[dbo].[myTable]);
Select COUNT(*) as [Count], Topic, (100.0*COUNT(*)/@Totals)/100.0 As Percentage
from FROM [myDatabase].[dbo].[myTable] group by Topic;

HTH
Ashok
0

LVL 41

Assisted Solution

Kyle Abrahams earned 1000 total points
ID: 37837681
declare @total int
set @total = (select count(*) from [myDatabase].[dbo].[myTable])

if @total > 0
select topic, cast(COUNT(topic) as [Count] as numeric(10,2))/ @total as percentage
FROM [myDatabase].[dbo].[myTable] group by Topic
else
select 'No Topics Found', 0
0

LVL 2

Expert Comment

ID: 37837696
You can try this...

Select (COUNT(*)/100)*100 as [Count], Topic FROM [myDatabase].[dbo].[myTable] group by Topic
0

Author Closing Comment

ID: 37838391
Thank you very much, your comments helped. After trying everything possible I settled with this one:

declare @total decimal

select @total = COUNT(*)
FROM [myDatabase].[dbo].[myTable]

Select COUNT(*) as [Count], cast(COUNT(*)/@total*100 as numeric(10,0)) as [Percentage], Topic
FROM [myDatabase].[dbo].[myTable]
group by Letter
0

LVL 41

Expert Comment

ID: 37838436
You may want to include that if statement as you have a potentional divide by zero error if MyTable has no rows.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
###### Suggested Courses
Course of the Month14 days, 5 hours left to enroll