[英]SQL - creating date range
I am using the following follow script to show me the cap_date and max_capacity in the future and ... and sub-queries to show me the last_date and last_qty where older than the first select statement. 我正在使用以下跟随脚本向我展示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)
Output from this script looks like ... 该脚本的输出看起来像...
... what I would like is to do is create a date list starting from the 'last_date' and show a qty for each date equal to last_qty; ...我要做的是创建一个从“ last_date”开始的日期列表,并为每个日期显示一个等于last_qty的数量; once the date list reaches the 'cap_date' qty should change to the max_capacity (the dates should run 7 days after the 'cap_date', ie 日期列表到达“ cap_date”后,数量应更改为max_capacity(日期应在“ cap_date”之后的7天运行,即
Any ideas? 有任何想法吗? Thanks 谢谢
First create a cal table containing the range of dates of interest. 首先创建一个包含感兴趣日期范围的校准表。 Lets call that table cal with one column dt of type date. 让我们将该表cal称为date类型的一列dt。 Assume your table above is called table_cap 假设您上面的表称为table_cap
Now do a 现在做一个
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.