提取一个月

时间:2016-04-13 15:19:25

标签: sql database oracle extract

找出过去最繁忙的宠物注册月份。我们假设注册的宠物越多,月份越忙。

SQL> select reg_month, registration
from 
(select to_char(dateRegistered,'dd-mon')reg_month,count(*) registration
from pet
group by to_char(dateRegistered,'dd-mon'))
where registration = (select max(registration)
from (select to_char(dateRegistered,'dd-mon')reg_month,count(*) registration
from pet
group by to_char(dateRegistered,'dd-mon')));



 REG_MONTH  REGISTRATION
 --------------- ------------
 12-nov            3

有人可以帮我提取一个月而不是一天。

1 个答案:

答案 0 :(得分:2)

Oracle支持标准SQL的EXTRACT,然后您可以使用RANK

select *
from
 ( select extract(month from dateRegistered) as mnth, 
      count(*) as cnt,
      rank() over (order by count(*) desc) as rnk
   from pet
   group by extract(month from dateRegistered)
 ) dt
where rnk = 1