简体   繁体   中英

How can I create oracle view to be dynamic?

create or replace view as
(select emp,department,salary
   from employee
   where partition_key = to_char(add_months(sysdate,-2) 'yyyymm'));

I have this view, where I get 2 months before data from employee table on 5th day of every month.

How can I use the same view to get last month's data like below from 10th day of every month.

create or replace view as
(select emp,department,salary
   from employee
   where partition_key = to_char(add_months(sysdate,-1) 'yyyymm'));

EDIT

From comments:

Basically, for the first 10 days of a month, I want to see 2 months before data and from 10th night, I want to see last month data.

Given your updated requirements it appears that the following will accomplish what you want:

create or replace view as
  select emp, department, salary
    from employee
    where partition_key =
            to_char(add_months(sysdate,
                               CASE
                                 WHEN TO_NUMBER(TRIM(TO_CHAR(SYSDATE, 'DD'))) <= 10
                                   THEN -2
                                 ELSE -1
                               END), 'yyyymm');

Best of luck.

It seems like you could subtract 10 days from the current date ...

create or replace view blah
as
select
  emp,
  department,
  salary
from
  employee
where
  partition_key = to_char(add_months(trunc(sysdate-10,'mm'),-1) 'yyyymm'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM