循环浏览日期范围

时间:2015-10-05 09:33:55

标签: oracle

如何通过日期循环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))
    ...

1 个答案:

答案 0 :(得分: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
;