将递增日期插入单独的Select查询中

时间:2018-04-27 09:35:07

标签: oracle plsql

我继承了以下查询: -

SELECT
 d.dt --date
,fuel --vchar
,SUM(revenue) revenue --number

FROM
    (
    SELECT
     ADD_MONTHS(TRUNC(SYSDATE,'mm'),-ROWNUM + 1)-1 dt     
    FROM dual    
    ) d

<<SNIP>> 

WHERE rn = 1    

GROUP BY
 d.dt
,fuel;

这将返回上个月的总和金额。

是否有可能以某种方式循环,以便返回特定历史日期的结果。一个静态的例子是: -

SELECT
 d.dt --date
,fuel --vchar
,SUM(revenue) revenue --number

FROM
    (
    SELECT to_date('31-08-2017', 'dd-MM-yyyy') dt     
    FROM dual    
    ) d

<<SNIP>> 

WHERE rn = 1    

GROUP BY
 d.dt
,fuel;

我已经能够编写代码来生成我感兴趣的日期: -

DECLARE
    startdate   DATE := TO_DATE('31/08/2016','dd/mm/yyyy');
    enddate     DATE := trunc(SYSDATE,'MM') - 1;
    usedate     DATE := enddate;
BEGIN
    LOOP
        usedate := add_months(usedate,-1);
        dbms_output.put_line(usedate);
        EXIT WHEN usedate <= startdate;
    END LOOP;
END;

...用usedate作为相关日期,但我不清楚如何在原始查询中使用这些日期。

2 个答案:

答案 0 :(得分:1)

您可以尝试如下。阅读内联评论。

DECLARE
  startdate DATE := TO_DATE('31/08/2016','dd/mm/yyyy');
  enddate   DATE := TRUNC(SYSDATE,'MM') - 1;
  usedate   DATE := enddate;
--Record to hold resultset from the Select query.
type UserInfo     IS      record
  (
    dt     DATE,
    fuel   VARCHAR2(100),
    revnue NUMBER );
TYPE v_UserInfo
IS
  TABLE OF USerinfo INDEX BY pls_integer;
  UserRecord UserInfo;
BEGIN
  LOOP
    usedate := add_months(usedate,-1);
    dbms_output.put_line(usedate);
    EXIT  WHEN usedate <= startdate;

    --Using usedate in select statement 

     SELECT * 
     BULK COLLECT    INTO UserRecord
    from (
          SELECT d.dt       ,
          fuel, --- This column is coming from another table
          SUM(revenue)      --- This column is coming from another table
     FROM
        ( SELECT ADD_MONTHS(TRUNC(usedate,'mm'),-ROWNUM + 1)-1 dt FROM dual
        ) d); --Make join with the table having columns in select query

      FOR i IN 1..UserRecord.count
      loop
       --Displaying records of select staement
       dbms_ouput.put_line(UserRecord(i).dt ||UserRecord(i).fuel || UserRecord(i).revnue);
      end loop;

  END LOOP;
END;

PS:未经测试。

答案 1 :(得分:0)

您错过了开始日期和使用日期之间的月数。不确定如何使用您的代码生成日期,如下所示:= add_months(usedate,-1);总会给你静态值= 28-FEB-18。 在我的例子中,我使用了一个不同的开始日期,我建议你进行测试:

 SELECT start_date
      , end_date
      , add_months(use_date,-LEVEL) stop_date
      , MONTHS_BETWEEN(use_date, start_date) months_diff -- increment number
  FROM
  (
   SELECT TO_DATE('31/08/2017','dd/mm/yyyy')   start_date 
        , trunc(SYSDATE,'MM')-1                end_date
        , add_months(trunc(SYSDATE,'MM')-1,-1) use_date
    FROM dual
 )
 CONNECT BY LEVEL <= MONTHS_BETWEEN(use_date, start_date) -- will run 6 times = months_diff --
/

START_DATE  END_DATE    STOP_DATE   MONTHS_DIFF
--------------------------------------------------
31-AUG-17   31-MAR-18   31-JAN-18   6
31-AUG-17   31-MAR-18   31-DEC-17   6
31-AUG-17   31-MAR-18   30-NOV-17   6
31-AUG-17   31-MAR-18   31-OCT-17   6
31-AUG-17   31-MAR-18   30-SEP-17   6
31-AUG-17   31-MAR-18   31-AUG-17   6  <<-- stopped here as stop_date equals to start date