Dividing 2 numbers returns 0

后端 未结 3 443
一向
一向 2021-01-28 06:48

I\'m trying to divide 2 counts in order to return a percentage.

The following query is returning 0:

select (
    (select COUNT(*) from sax         


        
3条回答
  •  清歌不尽
    2021-01-28 07:29

    It can be done more succinctly by moving the common condition to the where clause:

    select sum(case when endOfUse is null then 1 end) * 100.0 / count(*) percentage
    from saxref..AuthCycle
    where addDate >= '1/1/2014'
    

    Note how you don't need the case of 0 for false either, since nulls are ignored with sum()

提交回复
热议问题