简体   繁体   English

如何在SQL中划分以获得百分比?

[英]How to divide in SQL to get percentage?

I am using SSMS and trying to write a query that will calculate the percentage of closed cases within a certain time period. 我正在使用SSMS,并尝试编写一个查询,该查询将计算特定时间段内已结案的百分比。

For example, there is a column labeled "CLOSED", and within each row of that column is a date if the case is actually closed; 例如,有一列标记为“已关闭”的列,如果实际关闭该案例,则该列的每一行中都有一个日期; and if it is not closed, it will say NULL in the row, meaning the case is still open. 如果未关闭,则该行将显示NULL,表示该情况仍处于打开状态。

I am trying to divide the number of closed cases by the total number of cases in order to get a percentage of cases closed. 我正在尝试将结案数量除以案件总数,以得出结案百分比。

I was thinking about dividing where CLOSED is not NULL / total # of cases 我正在考虑划分CLOSED不为NULL的情况/案例总数

I just wasn't sure how I would go about this. 我只是不确定该如何处理。

Thanks! 谢谢!

Use conditional aggregation to divide the non-null rows with the total number of rows in the table. 使用条件聚合将非空行除以表中的总行数。

select sum(case when closed is not null then 1.0 end)/count(*) as closed_cases
from yourtable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM