Solved

SQL Percentage

Posted on 2012-04-12
5
272 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
5 Comments
 
LVL 13

Accepted Solution

by:
Ashok earned 250 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 40

Assisted Solution

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now