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

sqlsql-serversql-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,

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

but it returns 100 in the Total_Percentage column.

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

Thanks.

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