0

我在这个网站上找到了这个查询(虽然稍微修改了一下),它工作正常。但是,结果集没有显示 count(*) 为 0 的范围间隔。我尝试将 NVL 函数与左关节一起使用,但无法使其工作。有任何想法吗?(见下面的查询)

谢谢

select
    count(*) "# PRs Created",to_char(date_created, 'yyyy-mm-dd') as "Date Created",
    to_char(date_created, 'hh24') || ':00:00 - ' || to_char(date_created + 1/24, 'hh24') || ':00:00' RANGE 
    FROM pr
    where date_created between to_date(SYSDATE - 1000)and to_date(SYSDATE)
    and 1 * to_char(date_created, 'hh24') between 0 and 24
    group by to_char(date_created, 'hh24'), to_char(date_created + 1/24, 'hh24'),to_char(date_created, 'yyyy-mm-dd')
    order by to_char(date_created, 'yyyy-mm-dd'), RANGE  desc
4

1 回答 1

0

对于不存在的间隔,您不会得到零条目,因为没有什么可以计算的。要使其正常工作,您需要外部连接到日期时间表:

with dates as (
  select trunc(sysdate)-1000 + (rownum-1)/24 dt
  from   dual
  connect by level <= 1000 * 24
)
select dates.dt, count(pr.date_created) from pr, dates
where  trunc(pr.date_created (+), 'hh24')= dates.dt
group  by dates.dt;

with子句创建一个数据“表”,其中包含从sysdate-1000今天开始到现在的每一小时(查询中的条件)。您需要pr对此进行外部连接才能查看没有条目的间隔的行pr

于 2013-01-29T14:21:53.613 回答