在BMP 项目中统计在一个月内每周的在网站注册的人数

--------------------------------     这个统计是用来完成周统计,当前系统时间往前推几周的统计

---------从周日到 周六是个正周

-- 如下:是正确的

//body 定义

TYPE get_req_type IS RECORD(
    rate_range                       VARCHAR2(30)
     ,sum_regist1                      VARCHAR2(30)
    ,sum_regist2                    VARCHAR2(30));

// -------------------//

 

  PROCEDURE get_sum_data1(pi_file_id    IN bmp_extract_file.bef_file_id%TYPE
                         ,pi_sql        VARCHAR2
                         ,pio_rec_count IN OUT NUMBER) IS
    l_cursor SYS_REFCURSOR;
 
    fulfill_offline_rec get_req_type;
    l_out_line          VARCHAR2(32000) := NULL;
    -- l_table_index  INTEGER := pi_table_result.count;
    l_first_tj_count INTEGER := 0;
    l_sum_tj_count   INTEGER := 0;
    l_sequence       NUMBER := 1;
  BEGIN
    dbms_output.put_line(l_first_tj_count || '","');
    dbms_output.put_line('------ACTOS QUALITY HEALTH DEDUPE REPORT-----');
    OPEN l_cursor FOR pi_sql;
    LOOP
      FETCH l_cursor
        INTO fulfill_offline_rec;
      EXIT WHEN l_cursor%NOTFOUND;
      l_first_tj_count := l_first_tj_count +
                          fulfill_offline_rec.sum_regist1;
      l_sum_tj_count   := l_sum_tj_count + fulfill_offline_rec.sum_regist2;
      l_out_line       := l_sequence || '|' ||
                          bmp_util_pkg.remove_pipe(fulfill_offline_rec.rate_range) || '|' ||
                          bmp_util_pkg.remove_pipe(fulfill_offline_rec.sum_regist1) || '|' ||
                          bmp_util_pkg.remove_pipe(fulfill_offline_rec.sum_regist2);
   
      dbms_output.put_line(fulfill_offline_rec.rate_range || ' , ' ||
                           fulfill_offline_rec.sum_regist1 || ' , ' ||
                           fulfill_offline_rec.sum_regist2);
   
      bmp_util_extract_file_pkg.put_line(pi_file_id, l_out_line);
      l_sequence    := l_sequence + 1;
      pio_rec_count := pio_rec_count + 1;
    END LOOP;
 
    l_out_line := '   TOTAL             ' || ' | ' || l_first_tj_count ||
                  ' | ' || l_sum_tj_count;
    bmp_util_extract_file_pkg.put_line(pi_file_id, l_out_line);
    dbms_output.put_line('   TOTAL             ' || ' , ' ||
                         l_first_tj_count || ' , ' || l_sum_tj_count);
    CLOSE l_cursor;
 
  END get_sum_data1;
  --------------------
  PROCEDURE get_sum_week1(pi_file_id       IN bmp_extract_file.bef_file_id%TYPE
                         ,pi_sql           IN VARCHAR2
                         ,l_first_tj_count IN OUT NUMBER
                         ,l_sum_tj_count   IN OUT NUMBER
                         ,pio_rec_count    IN OUT NUMBER) IS
    l_cursor SYS_REFCURSOR;
 
    fulfill_offline_rec get_req_type;
    l_out_line          VARCHAR2(32000) := NULL;
 
    l_sequence       NUMBER := 1;
    l_frist_total    NUMBER := 0;
    l_no_frist_total NUMBER := 0;
  BEGIN
    OPEN l_cursor FOR pi_sql;
    LOOP
      FETCH l_cursor
        INTO fulfill_offline_rec;
      EXIT WHEN l_cursor%NOTFOUND;
   
      SELECT SUM(CASE
                   WHEN cr.bcr_first_registration = 'Y'
                        AND to_char(cr.bcr_registration_date, 'YYYYMMDD') BETWEEN
                        fulfill_offline_rec.sum_regist1 AND
                        fulfill_offline_rec.sum_regist2 THEN
                    1
                   ELSE
                    0
                 END) AS s1
            ,SUM(CASE
                   WHEN to_char(cr.bcr_registration_date, 'YYYYMMDD') BETWEEN
                        fulfill_offline_rec.sum_regist1 AND
                        fulfill_offline_rec.sum_regist2 THEN
                    1
                   ELSE
                    0
                 END) AS s2
        INTO l_frist_total
            ,l_no_frist_total
        FROM bmp_customer_registration cr
       WHERE 1 = 1;
      l_first_tj_count := l_first_tj_count + l_frist_total;
      l_sum_tj_count   := l_sum_tj_count + l_no_frist_total;
   
      l_out_line := l_sequence || '|' ||
                    bmp_util_pkg.remove_pipe(fulfill_offline_rec.rate_range) || '|' ||
                    bmp_util_pkg.remove_pipe(l_frist_total) || '|' ||
                    bmp_util_pkg.remove_pipe(l_no_frist_total);
   
      dbms_output.put_line(fulfill_offline_rec.rate_range || ' , ' ||
                           l_frist_total || ' , ' || l_no_frist_total);
   
      bmp_util_extract_file_pkg.put_line(pi_file_id, l_out_line);
      l_sequence    := l_sequence + 1;
      pio_rec_count := pio_rec_count + 1;
    END LOOP;
 
  END get_sum_week1;

  --**************************************************************************
  PROCEDURE gen_sum_detail_rec(po_file_id    OUT NUMBER
                              ,pi_start_date IN VARCHAR2
                              ,pi_week_month IN BOOLEAN DEFAULT TRUE
                              ,pio_rec_cnt   IN NUMBER) IS
    -- Type declarations
 
    c_proc CONSTANT VARCHAR2(30) := 'gen_email_detail_rec';
    lv_msg           VARCHAR2(4000);
    lv_locator       VARCHAR2(200) := 'Init';
    l_sql            VARCHAR2(32000) := '';
    l_out_file_id    bmp_extract_file.bef_file_id%TYPE;
    l_out_line       VARCHAR2(32000) := NULL;
    l_rec_count      NUMBER(7) := 0;
    l_first_tj_count INTEGER := 0;
    l_sum_tj_count   INTEGER := 0;
    --main control
  BEGIN
    lv_locator := '  Initialize variables..';
    --pio_rec_cnt := 0;
 
    lv_locator := '  Begin main loop..';
    IF pi_week_month
    THEN

-- 这里是按月统计的
      l_sql := 'SELECT to_char(cr.bcr_registration_date, ''yyyymm'') as d1
      ,SUM(CASE WHEN bcr_first_registration = ''Y''
                   AND to_char(cr.bcr_registration_date,''yyyymm'') BETWEEN  to_char(to_date(''' ||
               pi_start_date || ''', ''yyyymmdd''),''yyyymm'') AND   to_char(SYSDATE  ,''yyyymm'') THEN
               1
              ELSE
               0
            END) AS S1
            ,SUM(CASE WHEN  to_char(cr.bcr_registration_date,''yyyymm'') BETWEEN
                    to_char(to_date(''' || pi_start_date ||
               ''', ''yyyymmdd''),''yyyymm'')  AND
                   to_char(SYSDATE  ,''yyyymm'')  THEN
               1
              ELSE
               0
            END) AS S2
  FROM bmp_customer_registration cr
 GROUP BY to_char(cr.bcr_registration_date, ''yyyymm'')
 order by d1';
   
      lv_locator := 'Call procedure to generate fulfillment requests into file';
      dbms_output.put_line(substr(l_sql, 1, 4000));
      ---
      l_out_file_id := bmp_util_extract_file_pkg.open_file(c_cf_extract_type);
      l_out_line    := 'ACTOS QUALITY HEALTH DEDUPE REPORT|' ||
                       nvl(to_char(SYSDATE, l_date_format1), ' ') || '|' ||
                       c_brand;
   
      bmp_util_extract_file_pkg.put_line(l_out_file_id, l_out_line);
      l_out_line := 'Date Range|' || 'No of Registrations|' ||
                    'No of First Registrations';
   
      bmp_util_extract_file_pkg.put_line(l_out_file_id, l_out_line);
      get_sum_data1(l_out_file_id, l_sql, l_rec_count);
      lv_locator := 'end';
      l_out_line := 'TRL|' || l_rec_count;
      bmp_util_extract_file_pkg.put_line(l_out_file_id, l_out_line);
      bmp_util_extract_file_pkg.close_file(l_out_file_id);
      po_file_id := l_out_file_id;
   
    ELSE
      l_out_file_id := bmp_util_extract_file_pkg.open_file(c_cf_extract_type);
   
      dbms_output.put_line('------ACTOS QUALITY HEALTH DEDUPE REPORT-----');
   
      l_out_line := 'ACTOS QUALITY HEALTH DEDUPE REPORT|' ||
                    nvl(to_char(SYSDATE, l_date_format1), ' ') || '|' ||
                    c_brand;
   
      bmp_util_extract_file_pkg.put_line(l_out_file_id, l_out_line);
   
      lv_locator := '  Begin url loop ..';
      l_sql      := ' SELECT to_char(t.date_in_week, ''dd/MM/yyyy'') || ''-'' ||
               to_char(t.date_in_week + 6, ''dd/MM/yyyy'') AS rate_range
              ,to_char( t.date_in_week,''YYYYMMDD'') AS sum_regist1
              ,to_char(t.date_in_week + 6,''YYYYMMDD'') AS sum_regist2
          FROM (SELECT trunc(SYSDATE - ''' ||
                    pio_rec_cnt || ''' * 7, ''iw'') + rownum - 2 AS date_in_week
                  FROM all_objects
                 WHERE rownum <= (''' || pio_rec_cnt ||
                    ''' * 7)) t
         WHERE to_char(t.date_in_week, ''d'') = 1';
      dbms_output.put_line(l_sql);
      get_sum_week1(l_out_file_id, l_sql, l_first_tj_count, l_sum_tj_count, l_rec_count);
   
      l_out_line := '   TOTAL             ' || ' | ' || l_first_tj_count ||
                    ' | ' || l_sum_tj_count;
      bmp_util_extract_file_pkg.put_line(l_out_file_id, l_out_line);
      dbms_output.put_line('   TOTAL             ' || ' , ' ||
                           l_first_tj_count || ' , ' || l_sum_tj_count);
      l_out_line := 'TRL|' || l_rec_count;
      bmp_util_extract_file_pkg.put_line(l_out_file_id, l_out_line);
      bmp_util_extract_file_pkg.close_file(l_out_file_id);
      po_file_id := l_out_file_id;
   
    END IF;
 
  EXCEPTION
    WHEN OTHERS THEN
      lv_msg := bmp_util_err_pkg.format_err_msg(c_log_unit, c_proc, lv_locator, SQLERRM);
      logger.error(c_log_unit, lv_msg);
      bmp_util_err_pkg.raise(lv_msg);
  END gen_sum_detail_rec;

 

 

 

------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------

----------------下面的例子是不对的。只可以参考

 

//body 定义

TYPE get_req_type IS RECORD(
    rate_range                       VARCHAR2(30)
     ,sum_regist1                      VARCHAR2(30)
    ,sum_regist2                    VARCHAR2(30));

// -------------------//

 

  l_date_format         CONSTANT VARCHAR2(8) := 'YYYYMMDD';
  l_date_format1         CONSTANT VARCHAR2(10) := 'MM/DD/YYYY';

 

PROCEDURE get_sum_data1(pi_sql VARCHAR2) IS
    l_cursor SYS_REFCURSOR;
 
    fulfill_offline_rec get_req_type;
    -- l_table_index  INTEGER := pi_table_result.count;
    l_first_tj_count       INTEGER := 0;
    l_sum_tj_count         INTEGER := 0;
  BEGIN

    dbms_output.put_line('------ACTOS QUALITY HEALTH DEDUPE REPORT-----');
    OPEN l_cursor FOR pi_sql;
    LOOP
      FETCH l_cursor
        INTO fulfill_offline_rec;
      EXIT WHEN l_cursor%NOTFOUND;
      l_first_tj_count := l_first_tj_count + fulfill_offline_rec.sum_regist1;
      l_sum_tj_count :=l_sum_tj_count + fulfill_offline_rec.sum_regist2;
      dbms_output.put_line(fulfill_offline_rec.rate_range || ' | ' ||
                           fulfill_offline_rec.sum_regist1 || ' | ' ||
                           fulfill_offline_rec.sum_regist2);
   
    END LOOP;
    dbms_output.put_line('   TOTAL             '|| ' | ' ||l_first_tj_count|| ' | ' ||l_sum_tj_count);
    CLOSE l_cursor;
 
  END get_sum_data1;

 

------ACTOS QUALITY HEALTH DEDUPE REPORT----- 标题

----06/19/2011-06/25/2011 | 0 | 0
----06/12/2011-06/18/2011 | 6 | 6
----06/05/2011-06/11/2011 | 0 | 0
--------------   TOTAL              | 6 | 6

-----从周日到 周六是个正周


  --**************************************************************************
  --* Name   : gen_sum_detail_rec
  --* Author : ale
  --* Created: 07/06/2010
  --* Purpose: Exports Email Detail Report
  --* Input  : Brand Name
  --*          Extract Type
  --*          Start Date (MMDDYYYY)
  --* Output : file id
  --* Calls  :
  --* History Log:
  --* ------------------------------------------------------------------------
  --*    Date   |Developer| Rel        | Description
  --* ------------------------------------------------------------------------
  --* 07-06-2010| VC      | 05.28.00   | Initial
  --**************************************************************************

-----用迭代循环求多个UNION ALL相结合 特别要注意字符串拼接

-----根据参数“pio_rec_cnt” 来决定循环的次数 :也就是用几个周被统计
  PROCEDURE gen_sum_detail_rec(
                               -- pi_brand      IN bmp_brand.brd_brand%TYPE
                               pi_start_date IN VARCHAR2
                              ,pio_rec_cnt   IN NUMBER) IS
    -- Type declarations

    c_proc CONSTANT VARCHAR2(30) := 'gen_email_detail_rec';
    lv_msg     VARCHAR2(4000);
    lv_locator VARCHAR2(200) := 'Init';
    l_sql      VARCHAR2(32000) := '';
 
    --main control
  BEGIN
    lv_locator := '  Initialize variables..';
    --pio_rec_cnt := 0;
 
    lv_locator := '  Begin main loop..';
    FOR i IN 1 .. pio_rec_cnt
    LOOP
      lv_locator := '  Begin url loop ..';
      IF i > 1
      THEN
        l_sql := l_sql ||'  '|| 'Union All';

      END IF;
    ------ 这里是对l_sql 进行循环 我只要注意如何拼接就可以了。
      l_sql := l_sql  || ' SELECT  (to_char((next_day(to_date('''|| pi_start_date ||''', '''||l_date_format||'''), ''SUNDAY'') - '||i||' * 7 - 7), '''||l_date_format1||''')
           ||''-''|| to_char((next_day(to_date('''|| pi_start_date ||''', '''||l_date_format||'''), ''SUNDAY'') - '||i||' * 7 - 1), '''||l_date_format1||''')) AS date_range
          ,SUM( CASE
              WHEN cr.bcr_first_registration = ''Y''
                   AND to_char(cr.bcr_updated_date, '''||l_date_format||''') BETWEEN
                   to_char(next_day(to_date(''' ||
               pi_start_date || ''', '''||l_date_format||'''), ''SUNDAY'') - '||i||' * 7 - 7, '''||l_date_format||''') AND
                   to_char(next_day(to_date(''' ||
               pi_start_date || ''', '''||l_date_format||'''), ''SUNDAY'') - '||i||' * 7 - 1, '''||l_date_format||''') THEN
               1
              ELSE
               0
            END) AS sum_regist1
            ,SUM( CASE
              WHEN to_char(cr.bcr_updated_date, '''||l_date_format||''') BETWEEN
                   to_char(next_day(to_date(''' ||
               pi_start_date || ''', '''||l_date_format||'''), ''SUNDAY'') - '||i||' * 7 - 7, '''||l_date_format||''') AND
                   to_char(next_day(to_date(''' ||
               pi_start_date || ''', '''||l_date_format||'''), ''SUNDAY'') - '||i||' * 7 - 1, '''||l_date_format||''') THEN
               1
              ELSE
               0
            END) AS sum_regist2
   FROM bmp_customer_registration cr
  GROUP BY to_char(cr.bcr_updated_date, ''yyyymmdd-iw'')';
   
      IF i = pio_rec_cnt
      THEN
        l_sql := l_sql ||'  '||'   order by date_range desc ';
      END IF;
   
    END LOOP;
           -- dbms_output.put_line(l_sql);
    ---
    lv_locator := 'Call procedure to generate fulfillment requests into file';
    get_sum_data1(l_sql);
    lv_locator := 'end';
  EXCEPTION
    WHEN OTHERS THEN
      lv_msg := bmp_util_err_pkg.format_err_msg(c_log_unit, c_proc, lv_locator, SQLERRM);
      logger.error(c_log_unit, lv_msg);
      bmp_util_err_pkg.raise(lv_msg);
  END gen_sum_detail_rec;

 

 

----------------------------------------

 

// 上面的循环的结果类似如下:

  SELECT (to_char((next_day(to_date('20110701', 'yyyymmdd'), 'SUNDAY') - 3 * 7 - 7),'MM/DD/YYYY') ||' - ' || to_char((next_day(to_date('20110701', 'yyyymmdd'), 'SUNDAY') - 3 * 7 - 1),'MM/DD/YYYY')) AS date_range2
         ,SUM(CASE  WHEN cr.bcr_first_registration = 'Y'
                   AND to_char(cr.bcr_updated_date, 'yyyymmdd') BETWEEN
                   to_char(next_day(to_date('20110701', 'yyyymmdd'), 'SUNDAY') - 3 * 7 - 7, 'yyyymmdd') AND
                   to_char(next_day(to_date('20110701', 'yyyymmdd'), 'SUNDAY') - 3 * 7 - 1, 'yyyymmdd') THEN 1 ELSE 0 END) AS S1
                  
         ,SUM(CASE  WHEN to_char(cr.bcr_updated_date, 'yyyymmdd') BETWEEN '20110612' AND  '20110618' THEN 1 ELSE 0      END) AS s2  
   FROM bmp_customer_registration cr
  GROUP BY to_char(cr.bcr_updated_date, 'yyyymmdd-iw')  “UNION ALL”
order by date_range2 desc;

你可能感兴趣的:(sql,Date,exception,File,report,Integer)