簡體   English   中英

SQL-創建日期范圍

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM