報表需求:週一提取週五到週日的資料,其他時刻提取當天資料
阿新 • • 發佈:2019-02-17
現在的需求是,出一張日報,每天傳送的是前一天的資料。 如果是週末的話,不出資料,等在週一提取上週五到這週日的資料。 實現以上功能。 現在7月14,模擬7月10號的狀況 交易表資料 with invest as (select to_date('20170702', 'yyyymmdd') + level dt, level + 100 tranam from dual connect by level <= trunc(sysdate) - to_date('20170702', 'yyyymmdd')) select * from invest DT TRANAM ----------- ---------- 2017/7/3 101 2017/7/4 102 2017/7/5 103 2017/7/6 104 2017/7/7 105 2017/7/8 106 2017/7/9 107 2017/7/10 108 2017/7/11 109 2017/7/12 110 2017/7/13 111 2017/7/14 112 12 rows selected with invest as (select to_date('20170702', 'yyyymmdd') + level dt, level + 100 tranam from dual connect by level <= trunc(sysdate) - to_date('20170702', 'yyyymmdd')) select * from invest where dt between (case when to_char(sysdate - 4, 'd') in ('2') then trunc(sysdate - 4) - 3 else trunc(sysdate - 4)-1 end) and ( case when to_char(sysdate - 4, 'd') in ('2') then trunc(sysdate - 4) - 1 else trunc(sysdate-4)-1 end); DT TRANAM ----------- ---------- 2017/7/7 105 2017/7/8 106 2017/7/9 107 7月11的時候是星期二:傳送的資料應該是前一天的資料10號的 with invest as (select to_date('20170702', 'yyyymmdd') + level dt, level + 100 tranam from dual connect by level <= trunc(sysdate) - to_date('20170702', 'yyyymmdd')) select * from invest where dt between (case when to_char(to_date('20170711','yyyymmdd') - 3, 'd') in ('2') then to_date('20170711','yyyymmdd') - 3 else to_date('20170711','yyyymmdd')-1 end) and ( case when to_char(to_date('20170711','yyyymmdd') - 3, 'd') in ('2') then to_date('20170711','yyyymmdd')- 1 else to_date('20170711','yyyymmdd')-1 end) DT TRANAM ----------- ---------- 2017/7/10 108