[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
5
Medium Priority
?
279 Views
Last Modified: 2012-04-12
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?
Thanks for your help.
0
Comment
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
  • Learn & ask questions
5 Comments
 
LVL 13

Accepted Solution

by:
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

by:Kyle Abrahams
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

by:Mohamed_allabakash
ID: 37837696
You can try this...

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

Author Closing Comment

by:baxtalo
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

by:Kyle Abrahams
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
Video by: ITPro.TV
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

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question