如何创建动态的 oracle 视图?
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'));
我有这个观点,我在每个月的第 5 天从员工 table 那里获得 2 个月前的数据。
如何使用相同的视图从每个月的第 10 天获取上个月的数据,如下所示。
create or replace view as
(select emp,department,salary
from employee
where partition_key = to_char(add_months(sysdate,-1) 'yyyymm'));
编辑
来自评论:
基本上,对于一个月的前 10 天,我想查看前 2 个月的数据,从第 10 天晚上开始,我想查看上个月的数据。
根据您更新后的要求,以下内容似乎可以满足您的要求:
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');
祝你好运。
您似乎可以从当前日期减去 10 天...
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'));
create or replace view as
(select emp,department,salary
from employee
where partition_key = to_char(add_months(sysdate,-2) 'yyyymm'));
我有这个观点,我在每个月的第 5 天从员工 table 那里获得 2 个月前的数据。
如何使用相同的视图从每个月的第 10 天获取上个月的数据,如下所示。
create or replace view as
(select emp,department,salary
from employee
where partition_key = to_char(add_months(sysdate,-1) 'yyyymm'));
编辑
来自评论:
基本上,对于一个月的前 10 天,我想查看前 2 个月的数据,从第 10 天晚上开始,我想查看上个月的数据。
根据您更新后的要求,以下内容似乎可以满足您的要求:
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');
祝你好运。
您似乎可以从当前日期减去 10 天...
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'));