ORACLE SQL:填写缺少的日期 [英] ORACLE SQL: Fill in missing dates
问题描述
我有以下代码,可以为我提供三十天的生产日期和生产量.
I have the following code which gives me production dates and production volumes for a thirty day period.
select
(case when trunc(so.revised_due_date) <= trunc(sysdate)
then trunc(sysdate) else trunc(so.revised_due_date) end) due_date,
(case
when (case when sp.pr_typ in ('VV','VD') then 'DVD' when sp.pr_typ in ('RD','CD')
then 'CD' end) = 'CD'
and (case when so.tec_criteria in ('PI','MC')
then 'XX' else so.tec_criteria end) = 'OF'
then sum(so.revised_qty_due)
end) CD_OF_VOLUME
from shop_order so
left join scm_prodtyp sp
on so.prodtyp = sp.prodtyp
where so.order_type = 'MD'
and so.plant = 'W'
and so.status_code between '4' and '8'
and trunc(so.revised_due_date) <= trunc(sysdate)+30
group by trunc(so.revised_due_date), so.tec_criteria, sp.pr_typ
order by trunc(so.revised_due_date)
我的问题是,有没有计划生产的日期,该日期将不会出现在报告中.有没有办法填补缺失的日期.
The problem I have is where there is a date with no production planned, the date wont appear on the report. Is there a way of filling in the missing dates.
即当前报告显示以下内容...
i.e. the current report shows the following ...
DUE_DATE CD_OF_VOLUME
14/04/2015 35,267.00
15/04/2015 71,744.00
16/04/2015 20,268.00
17/04/2015 35,156.00
18/04/2015 74,395.00
19/04/2015 3,636.00
21/04/2015 5,522.00
22/04/2015 15,502.00
04/05/2015 10,082.00
注意:缺少日期(2015年4月20日,2015年4月23日至2015年3月5日)
Note: missing dates (20/04/2015, 23/04/2015 to 03/05/2015)
范围从sysdate开始始终为三十天. 您如何填写缺失的日期? 您需要某种日历表吗?
Range is always for a thirty day period from sysdate. How do you fill in the missing dates? Do you need some kind of calendar table?
谢谢
推荐答案
您可以通过以下方式从SYSDATE
获取30天期限(我假设您想包含SYSDATE
?):
You can get the 30-day period from SYSDATE
as follows (I assume you want to include SYSDATE
?):
WITH mydates AS (
SELECT TRUNC(SYSDATE) - 1 + LEVEL AS due_date FROM dual
CONNECT BY LEVEL <= 31
)
然后使用上面的代码对查询执行LEFT JOIN
(将查询放在CTE中也可能不是一个坏主意):
Then use the above to do a LEFT JOIN
with your query (perhaps not a bad idea to put your query in a CTE as well):
WITH mydates AS (
SELECT TRUNC(SYSDATE) - 1 + LEVEL AS due_date FROM dual
CONNECT BY LEVEL <= 31
), myorders AS (
select
(case when trunc(so.revised_due_date) <= trunc(sysdate)
then trunc(sysdate) else trunc(so.revised_due_date) end) due_date,
(case
when (case when sp.pr_typ in ('VV','VD') then 'DVD' when sp.pr_typ in ('RD','CD')
then 'CD' end) = 'CD'
and (case when so.tec_criteria in ('PI','MC')
then 'XX' else so.tec_criteria end) = 'OF'
then sum(so.revised_qty_due)
end) CD_OF_VOLUME
from shop_order so
left join scm_prodtyp sp
on so.prodtyp = sp.prodtyp
where so.order_type = 'MD'
and so.plant = 'W'
and so.status_code between '4' and '8'
and trunc(so.revised_due_date) <= trunc(sysdate)+30
group by trunc(so.revised_due_date), so.tec_criteria, sp.pr_typ
order by trunc(so.revised_due_date)
)
SELECT mydates.due_date, myorders.cd_of_volume
FROM mydates LEFT JOIN myorders
ON mydates.due_date = myorders.due_date;
如果要在缺失"日期显示零而不是NULL
,请使用上面的COALESCE(myorders.cd_of_volume, 0) AS cd_of_volume
.
If you want to show a zero on "missing" dates instead of a NULL
, use COALESCE(myorders.cd_of_volume, 0) AS cd_of_volume
above.
这篇关于ORACLE SQL:填写缺少的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!