我在SQL Server 2012中有一个表,其中包含如下数据:
Request_Type Total_Count --------------------------- General 25 Inquiry 15
我想像这样在最后一列中返回计数的百分比:
Request_Type Total_Count Total_Percentage -------------------------------------------------- General 25 62.5 Inquiry 15 37.5
我尝试了以下查询,
select Request_Type, Total_Count, (Total_Count* 100) / isnull(sum(Total_Count),0) as Total_Percentage from tbl_Request
但它在Total_Percentage列中返回100 。
Total_Percentage
任何关于如何获得结果的想法将不胜感激。
谢谢。
您可以使用子查询获取SUM(Total_Count):
SUM(Total_Count)
SELECT Request_Type , Total_Count , (Total_Count * 100) / (SELECT SUM(Total_Count) FROM tbl_Request) AS Total_Percentage FROM tbl_Request