-1

How to loop Oracle query through the date? I have to put variable in 4 place. My query start with WITH AS, so I can't use Oracle SQL Loop through Date Range solution.

I also can't create temporary table.

Here is my attempt:

   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))
    ...
Community
  • 1
  • 1
Kulis
  • 988
  • 3
  • 11
  • 25

1 Answers1

1

If I understand you correctly, you assume that you can only use 1 inline table per query. That is not true, you can use multiple inline tables and expand the existing WITH clause with another to loop through dates:

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
;
Martin Schapendonk
  • 12,893
  • 3
  • 19
  • 24