【问题标题】:Oracle/SQL: How can I show dates and counts even when count is zero?Oracle/SQL:即使计数为零,如何显示日期和计数?
【发布时间】:2016-02-25 16:38:51
【问题描述】:

所以,我需要查询一个表并显示每天的计数,即使该计数为零。我尝试了类似下面的方法,但它没有显示计数为零的天数。有人有其他想法吗?使用甲骨文,顺便说一句。非常感谢!!

with the_dates as (
  select to_date('080114','MMDDYY') + level - 1 as the_date
    from dual
 connect by level <= to_date('011716', 'MMDDYY') 
                     - to_date('080114', 'MMDDYY') + 1
         )
select distinct trunc(a.the_date), count(*)
from the_dates a
left outer join TableFoo f on a.the_date = to_date(admit_date, 'MMDDYYYY')
where f.customer_num = 10
group by trunc(a.the_date)
order by trunc(a.the_date);

【问题讨论】:

    标签: sql oracle date count


    【解决方案1】:

    问题是您的where 子句将left join 变成了inner join。所以:

    with . . .
    select trunc(a.the_date), count(f.customer_num)
    from the_dates a left outer join
         TableFoo f
         on a.the_date = to_date(admit_date, 'MMDDYYYY') and
            f.customer_num = 10
    group by trunc(a.the_date)
    order by trunc(a.the_date);
    

    此外,在使用group by 时几乎不需要select distinct(在这种情况下当然不需要)。

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 2015-07-07
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多