您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

SQL-填写没有结果的日期

SQL-填写没有结果的日期

根据您想要创建它们的方式,您可以创建一个日历表或使用Oracleconnect by语法动态生成行。

with the_dates as (
  select max(trunc(Create_Dtime)) as max_date
       , min(trunc(Create_Dtime)) as min_date
    from player 
         )
  , generator as (
  select min_date + level as the_date
    from the_dates
 connect by level <= max_date
         )
select g.the_date, count(trunc(p.Create_Dtime))
  from generator g
  left outer join player p
    on g.the_date = trunc(p.Create_Dtime)
 group by g.the_date
 order by g.the_date desc

如果您选择了“压光机表格”选项,则它会更清洁一些:

with the_dates as (
  select max(trunc(Create_Dtime)) as max_date
       , min(trunc(Create_Dtime)) as min_date
    from player 
         )
select c.the_date, count(trunc(p.Create_Dtime))
  from calender c
  join the_dates td
    on c.the_date between td.min_date and td.max_date
  left outer join join player p
    on c.the_date = trunc(p.Create_Dtime)
 group by c.the_date
 order by c.the_date

或者,刚刚注意到您的日期限制:

with the_dates as (
  select to_date('07-05-2012','dd-mm-yyyy') + level as the_date
    from dual
 connect by level <= trunc(to_date('07-05-2012','dd-mm-yyyy') - sysdate)
         )
 select td.the_date, count(trunc(p.create_dtime))
   from the_dates td
   left outer join player p
     on td.the_date = trunc(p.create_dtime)
  group by td.the_date
  order by td.the_date

对于所有这些,我建议trunc(create_dtime)在您的player表上建立一个索引。

SQLServer 2022/1/1 18:53:10 有369人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶