[英]SQL - creating date range
我正在使用以下跟随脚本向我展示cap_date和max_capacity在将来,以及...和子查询向我展示last_date和last_qty比第一个select语句还早。
select
trunc(gp.cap_date) cap_date, gp.max_capacity,
(select max(trunc(gp1.cap_date))
from GPS_CAPACITY gp1
where gp.plant = gp1.plant
and gp.work_center_no = gp1.work_center_no
and gp1.cap_date = (select max(gp2.cap_date)
from GPS_CAPACITY gp2
where gp.plant = gp2.plant
and gp.work_center_no = gp2.work_center_no
and gp2.cap_date < gp.cap_date)) last_date,
(select max(gp1.max_capacity)
from GPS_CAPACITY gp1
where gp.plant = gp1.plant
and gp.work_center_no = gp1.work_center_no
and gp1.cap_date = (select max(gp2.cap_date)
from GPS_CAPACITY gp2
where gp.plant = gp2.plant
and gp.work_center_no = gp2.work_center_no
and gp2.cap_date < gp.cap_date)) last_qty
from
GPS_CAPACITY gp
where
gp.plant = 'W'
and gp.work_center_no = 'HPKG'
and trunc(gp.cap_date) > trunc(sysdate)
该脚本的输出看起来像...
...我要做的是创建一个从“ last_date”开始的日期列表,并为每个日期显示一个等于last_qty的数量; 日期列表到达“ cap_date”后,数量应更改为max_capacity(日期应在“ cap_date”之后的7天运行,即
有任何想法吗? 谢谢
首先创建一个包含感兴趣日期范围的校准表。 让我们将该表cal称为date类型的一列dt。 假设您上面的表称为table_cap
现在做一个
select cal.dt, case when cal.dt<cap_date then qty else max_capacity end
from cal
inner join table_cap on
(table_cap.last_date <= cal.dt
and cal.dt < adddate(capdate, interval 7 day )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.