Solved

SQL Percentage

Posted on 2012-04-12
5
271 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 39

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 39

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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