Solved

SQL Percentage

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access is denied error from google drive api v2 in asp.net 1 68
Query in AngularJS 8 77
e commerce steps shown instead of self testing 2 49
async questions 5 45
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

742 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