簡體   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'));

我有這種觀點,在每個月的第5天,我從employee表中獲得數據之前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'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM