简体   繁体   English

如何创建动态的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'));

I have this view, where I get 2 months before data from employee table on 5th day of every month. 我有这种观点,在每个月的第5天,我从employee表中获得数据之前2个月。

How can I use the same view to get last month's data like below from 10th day of every month. 我如何使用同一视图从每个月的第10天获取上个月的数据,如下所示。

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. 基本上,对于一个月的前10天,我想查看数据之前的2个月,而从10日晚上开始,我想查看上个月的数据。

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 ... 看来您可以从当前日期减去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'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Oracle中创建动态视图 - create a dynamic view in Oracle 如何为Oracle创建动态WHERE语句 - How can I create a dynamic WHERE statement for Oracle 如何根据用户在 Oracle 中的输入创建动态视图? - How to create a dynamic view based on the input from a user in Oracle? 如何在两个表相互依赖的两个表中创建动态插入? (Oracle SQL) - How can i create a dynamic insert into two tables where both tables depend on each other? (oracle sql) 我如何优化视图 oracle sql - how can i optimize view oracle sql 如何在 Oracle 中创建包含内容取决于相关实体组合值的列的视图? - How can I create a View in Oracle with a column whose content depends on the combined values of pertaining entities? Oracle SQL:如何使用表中的日期字段创建日期表或日期视图 - Oracle SQL: How can I create a date table or date view using a date field in a table 如何在 Oracle 中使用其内容从相关实体的数值聚合的字符串列创建视图? - How can I create a View in Oracle with a String column whose content is aggregated from numeric values of related entities? 如何在Oracle SQL中使用动态数据创建替代替换? - How do I create an alter replace in Oracle SQL with dynamic data? 如何在 Oracle SQL 中创建包含三个表的视图? - How do I create a view with three tables in Oracle SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM