2015-10-05 141 views
-1

如何通過日期循環Oracle查詢?我必須把變量放在4個地方。我的查詢從WITH AS開始,所以我不能使用Oracle SQL Loop through Date Range解決方案。循環日期範圍

我也不能創建臨時表。

這裏是我的嘗試:

WITH d 
AS (
    SELECT DATE'2015-06-22' + LEVEL - 1 AS current_d 
     FROM dual 
    CONNECT BY DATE'2015-06-22' + LEVEL - 1 < DATE'2015-10-04' 
), 
OrderReserve 
AS (
    SELECT cvwarehouseid 
     ,lproductid 
     ,SUM(lqty) lqty 
    FROM ABBICS.iOrdPrdQtyDate 
    GROUP BY cvwarehouseid 
     ,lproductid 
    ) 
    SELECT 
    ... 
    WHERE IORDREFILL.DNCONFIRMEDDELDATE < CAST(TO_CHAR(d.current_d , 'YYYYMMDD') AS NUMBER(38)) 
    ... 
+0

檢查:http://stackoverflow.com/questions/987610/how-to-iterate -over-a-date-range-in-pl-sql –

+0

在此處放置一些示例.. – San

+0

在上述查詢中,Oracle不識別d.current_d – Kulis

回答

1

如果我理解正確的話,你認爲你只能使用每個查詢1個內嵌表。這是不正確的,你可以通過日期使用多個內嵌表和擴大現有WITH條款與另一個循環:

with OrderReserve as (
    SELECT cvwarehouseid 
     ,lproductid 
     ,SUM(lqty) lqty 
    FROM ABBICS.iOrdPrdQtyDate 
    GROUP BY cvwarehouseid 
     ,lproductid  
), date_range as (
    select sysdate+level 
    from dual 
    connect by level <= 30 
) 
select * 
from OrderReserve, date_range 
... -- expand with date_range as you see fit 
;