SQL - 划分两个结果

Ame*_*ico 7 sql oracle oracle-sqldeveloper

我下面有两个查询,两个都来自同一个"玩家"表.我想通过查询2划分查询1以获得相关百分比.我相对较新的更详细的SQL查询,以及在论坛上发布...但请告诉我,如果你有任何建议如何结合这来获得相关的百分比结果.

1

Select
  sysdate,sum(Count(init_dtime))
From Player p
Where
  Trunc(Init_Dtime) > Trunc(Sysdate) - 7 
  And Trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd')
  and trunc(create_dtime) < to_date('2015-sep-9','yyyy-mon-dd')
Group By Trunc(Init_Dtime)
Order By Trunc(Init_Dtime) Asc
Run Code Online (Sandbox Code Playgroud)

2

Select
  Sum(Count(Create_Dtime))
From Player P
where 
  Trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd')
  And Trunc(Create_Dtime) < To_Date('2015-sep-9','yyyy-mon-dd')
Group By Trunc(create_Dtime)
Order By Trunc(create_Dtime) Asc
Run Code Online (Sandbox Code Playgroud)

Niv*_*vas 5

你可以说

select sysdate,
       count((init_dtime)) / sum((Create_Dtime)) * 100 as percentage
  from Player p
 where Trunc(Init_Dtime) > Trunc(Sysdate) - 7 
   and Trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd')
   and trunc(create_dtime) < to_date('2015-sep-9','yyyy-mon-dd')
   order by percentage asc
Run Code Online (Sandbox Code Playgroud)

group by在SQL后不需要你不是真正的东西分组。group by例如,当您需要按玩家划分百分比时,此功能很有用。然后,您会说group by player_id,在select中将具有player_id

select player_id, count(…)
  from …
 where …
group by player_id
Run Code Online (Sandbox Code Playgroud)

编辑:如果where子句不同:

select sysdate, 
       (
           (select count((init_dtime))
             from player p
            where trunc(Init_Dtime) > trunc(Sysdate) - 7 
              and Trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd')
              and trunc(create_dtime) < to_date('2015-sep-9','yyyy-mon-dd'))
            / 
           (select count((Create_Dtime))
              from player P
             where trunc(Create_Dtime) >= To_Date('2012-mar-01','yyyy-mon-dd')
               and trunc(Create_Dtime) < To_Date('2015-sep-9','yyyy-mon-dd'))
       ) * 100 as percentage
from dual
Run Code Online (Sandbox Code Playgroud)