如何填写空白记录之间的缺失日期?

时间:2018-09-25 06:39:24

标签: sql oracle

我正在尝试在空白记录之间填充日期,但是没有成功。试图做多个选择方法,试图加入,但似乎我错过了重点。我想生成缺少日期的记录,从此代码块生成图表。首先,我希望“手动”填写日期,之后,我将重新组织这段代码并将该方法替换为参数。

有人可以帮我这个表情吗?

SELECT
    LOG_LAST AS "data",
    SUM(run_cnt) AS "Number of runs"

FROM

dual l
LEFT OUTER JOIN "LOG_STAT" stat ON
stat."LOG_LAST" = l."CLASS"

WHERE

new_class = '$arg[klasa]'



--SELECT to_date(TRUNC (SYSDATE - ROWNUM), 'DD-MM-YYYY'),
--0
 --FROM dual CONNECT BY ROWNUM < 366

GROUP BY
    LOG_LAST
ORDER BY

LOG_LAST

//编辑:

LOG_LAST只是一个带有日期的列(例如:25.04.2018 15:44:21),run_cnt是一个只有简单数字的列,LOG_STAT是一个包含LOG_LAST和run_cnt的表,new_class是一个带有名称的列我想列出的记录,即使它们不存在。例如:我有一条记录,记录的日期为24-09-2018、23-09-2018、20-09-2018、18-09-2018,并且我想列出没有名称和run_cnt的记录,但会丢失某个时期的日期

3 个答案:

答案 0 :(得分:0)

尝试填充isull:

SELECT
case when   trim(LOG_LAST) is null then '01-01-2018' 
 else isnull(LOG_LAST,'01-01-2018')end AS data,
    SUM(isnull(run_cnt,0)) AS "Number of runs"

FROM

dual l
LEFT OUTER JOIN "LOG_STAT" stat ON
stat."LOG_LAST" = l."CLASS"

WHERE

new_class = '$arg[klasa]'



--SELECT to_date(TRUNC (SYSDATE - ROWNUM), 'DD-MM-YYYY'),
--0
 --FROM dual CONNECT BY ROWNUM < 366

GROUP BY
    LOG_LAST
ORDER BY

LOG_LAST

答案 1 :(得分:0)

它称为数据密集化。来自oracle doc Data Densification for Reporting的示例data densification

with ls as
(
  select trunc(created) as day,object_type new_class, sum(1)  as total
  from user_objects  
  group by trunc(created),object_type
)
, all_dates(day) as
(
  select min(day) from ls
  union all
  select day + 1 from all_dates where day < (select max(day) from ls)
)
select d.day, nvl(ls.total,0),new_class
from all_dates d 
left join ls partition by (ls.new_class)  on ls.day = d.day
order by d.day;

答案 2 :(得分:0)

您想要的东西或多或少:

select d.day, sum(ls.run_cnt)
from all_dates d
left join log_stat ls on trunc(ls.log_last) = d.day
where ls.new_class = :klasa
group by d.day
order by d.day;

上面查询中的all_dates表应该包含所有以最小klasa log_last日期开始,以最大klasa log_last日期结束的日期。您可以通过递归查询获得这些日期。

with ls as
(
  select trunc(log_last) as day, sum(run_cnt) as total
  from log_stat
  where new_class = :klasa
  group by trunc(log_last)
)
, all_dates(day) as
(
  select min(day) from ls
  union all
  select day + 1 from all_dates where day < (select max(day) from ls)
)
select d.day, ls.total
from all_dates d
left join ls on ls.day = d.day
order by d.day;