Sql – How to calculate percentage of each row in SQL Server 2012


I have a table in SQL Server 2012 with data like this:

Request_Type    Total_Count
   General         25
   Inquiry         15

I want to return the percentage of counts in the last column like this:

Request_Type    Total_Count    Total_Percentage
   General         25              62.5
   Inquiry         15              37.5

I tried the following query,

    Request_Type, Total_Count, 
    (Total_Count* 100) / isnull(sum(Total_Count),0) as Total_Percentage 

but it returns 100 in the Total_Percentage column.

Any ideas on how to achieve the result will be appreciated.


Best Solution

You can use subquery to get the SUM(Total_Count):

SELECT Request_Type
     , Total_Count
     , (Total_Count * 100) / (SELECT SUM(Total_Count) FROM tbl_Request) AS Total_Percentage
FROM tbl_Request