小编典典

除以2的数字将返回0

sql

我试图除以2计数以返回一个百分比。

返回以下查询0

select (
    (select COUNT(*) from saxref..AuthCycle
         where endOfUse is null and addDate >= '1/1/2014') / 
    (select COUNT(*) from saxref..AuthCycle 
         where addDate >= '1/1/2014')
    ) as Percentage

我应该应聘演员吗?


阅读 222

收藏
2021-04-14

共1个答案

小编典典

我会用两个sums来做不同的事情:

select sum
       ( case
         when endOfUse is null and addDate >= '1/1/2014'
         then 1
         else 0
         end
       )
       * 100.0 -- if you want the usual 0..100 range for percentages
       /
       sum
       ( case
         when addDate >= '1/1/2014'
         then 1
         else 0
         end
       )
       percentage
from   saxref..AuthCycle
2021-04-14