gpt4 book ai didi

sql - ORACLE SQL : Fill in missing dates

转载 作者:行者123 更新时间:2023-12-01 08:14:53 24 4
gpt4 key购买 nike

我有以下代码,它为我提供了三十天的生产日期和生产量。

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)

我遇到的问题是有一个没有计划生产的日期,该日期不会出现在报告中。有没有办法填写缺失的日期。

即当前报告显示以下内容......
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

注意:缺少日期(20/04/2015、23/04/2015 至 03/05/2015)

范围始终是从 sysdate 算起的三十天。
你如何填写缺少的日期?
你需要某种日历表吗?

谢谢

最佳答案

您可以从 SYSDATE 获取 30 天期限,如下所示(我假设您想包括 SYSDATE ?):

WITH mydates AS (
SELECT TRUNC(SYSDATE) - 1 + LEVEL AS due_date FROM dual
CONNECT BY LEVEL <= 31
)

然后使用上面的代码对您的查询执行 LEFT JOIN (将您的查询放在 CTE 中也许不是一个坏主意):
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

关于sql - ORACLE SQL : Fill in missing dates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29634180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com