简体   繁体   中英

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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