如何计算 SQL Server 2012 中每行的百分比? [英] How to calculate percentage of each row in SQL Server 2012?

查看:50
本文介绍了如何计算 SQL Server 2012 中每行的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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

我尝试了以下查询,

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

但它在 Total_Percentage 列中返回 100.

but it returns 100 in the Total_Percentage column.

关于如何实现结果的任何想法将不胜感激.

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

谢谢.

推荐答案

可以使用子查询得到SUM(Total_Count):

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

这篇关于如何计算 SQL Server 2012 中每行的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆