SQL - 以百分比显示结果

时间:2012-09-17 05:23:41

标签: sql oracle

select retention
 , Sum(count(retention)) over(order by retention desc) as Num_Users_Retained
  from (select player_id
         , round(init_dtime-create_dtime,0) as retention
      from player
     where Trunc(Create_Dtime) >= To_Date('2012-jan-01','yyyy-mon-dd')
       and init_dtime is not null)
  Group by retention
  order by retention

此查询显示两列:第一列是今年的第一天(第0天是jan-1,第259天是今天的sep-16),第二列是保留。第0天有428,000,因为所有用户都被保留,第1天有300K(因为128K人在第一天之后没有登录),随着时间的推移,这个数字不断减少。我想为每天的第0天百分比显示另一列。这是否足够清楚,还是应该提供更多详细信息?

1 个答案:

答案 0 :(得分:2)

select retention
     , Num_Users_Retained
     , round(Num_Users_Retained/max(Num_Users_Retained) over() * 100, 2) as perc
  from (select retention
             , Sum(count(retention)) over(order by retention desc) as Num_Users_Retained
         from (select player_id
                    , round(init_dtime-create_dtime,0) as retention
                from player
               where Trunc(Create_Dtime) >= To_Date('2012-jan-01','yyyy-mon-dd')
                 and init_dtime is not null
              )
        Group by retention
        order by retention)
 order by retention