首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SQL -创建日期范围

SQL -创建日期范围
EN

Stack Overflow用户
提问于 2014-10-24 00:51:28
回答 1查看 78关注 0票数 1

我正在使用下面的脚本向我展示未来的cap_date和max_capacity ...以及子查询,向我显示比第一个select语句旧的last_date和last_qty。

代码语言:javascript
运行
复制
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_max_capacity‘之后7天运行,即

有什么想法吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-12-23 22:12:05

首先创建一个包含感兴趣日期范围的cal表。让我们用一个date类型列dt调用这个表。假设上面的表名为table_cap

现在做一个

代码语言:javascript
运行
复制
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 )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26533233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档