2

我有一个物化视图表,其中包含有关员工及其对系统的访问的信息,该表有一个“有效日期”列和“有效日期”列。这个物化视图每晚更新一次。

我想在此表中添加一个额外的列“current_flag”,以显示当前员工是否具有“Y”或“N”的访问权限。我正在考虑使用类似以下的“案例”声明:-

CASE WHEN  to_date(c.effective_date) < to_date(sysdate) 
and to_date      (effective_date_to) > to_date(sysdate)
THEN  'Y'
   ELSE 'N'            
END) AS current_flag

这是最好的方法吗?如果是,正确的语法是什么,因为我正在努力让它工作..

原始物化视图表如下...

CREATE MATERIALIZED VIEW TC.employee_access 

(
EMPL_ID,
EFFECTIVE_DATE,
EFFECTIVE_DATE_TO,
DEPARTMENT,
JOB_DESCRIPTION,
TIME_STAMP,
MODIFIED_BY
)
TABLESPACE TC_TC_DATA
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          64K
            NEXT             1M
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
           )
NOCACHE
LOGGING
NOCOMPRESS
NOPARALLEL
BUILD IMMEDIATE
REFRESH FORCE
START WITH TO_DATE('16-Jan-2013','dd-mon-yyyy')
NEXT TRUNC(SYSDATE+1)
WITH PRIMARY KEY
AS 

SELECT C.EMPL_ID AS Empl_Id,
       C.EFFECTIVE_DT AS Effective_Date,
       DECODE (n.effective_dt,
               c.effective_dt, TO_DATE ('01/01/2099', 'DD/MM/YYYY'),
               n.effective_dt - 1)
              AS Effective_Date_To,
       C.DFLT_UDT06_ID AS Department,     
       C.DFLT_UDT08_ID AS Job_Description,
       C.TIME_STAMP,
       C.MODIFIED_BY
  FROM empl_history_mv c, empl_history_mv n
 WHERE n.effective_dt =
          (SELECT NVL (MIN (effective_dt), c.effective_dt)
             FROM empl_history_mv
            WHERE empl_id = c.empl_id AND effective_dt > c.effective_dt)
       AND c.empl_id = n.empl_id;
4

2 回答 2

0

-statementCASE需要访问您的计算值effective_date_to。您可以使用一组额外的括号来执行此操作:

SELECT empl_id,
       effective_date,
       effective_date_to,
       CASE WHEN effective_date < SYSDATE AND SYSDATE < effective_date_to
            THEN 'Y' ELSE 'N' END current_flag
  FROM (
        SELECT c.empl_id
               c.effective_dt as effective_date,
          FROM ...
       );

或者您将逻辑展开到一个查询中:

SELECT c.empl_id, 
       c.effective_dt as effective_date,
       NVL2(n.effective_dt, n.effective_dt - 1, DATE'2099-01-01') AS effective_date_to,
       CASE WHEN c.effective_dt <= sysdate
             AND (sysdate < n.effective_dt OR n.effective_dt IS NULL) 
            THEN 'Y' ELSE 'N' END AS current_flag
  FROM empl_history_mv c
  LEFT JOIN empl_history_mv n 
    ON c.empl_id = n.empl_id
   AND c.effective_dt < n.effective_dt
   AND NOT EXISTS (
       SELECT * 
         FROM empl_history_mv t
        WHERE c.empl_id = t.empl_id
          AND c.effective_dt < t.effective_dt
          AND t.effective_dt < n.effective_dt
       );
于 2013-01-15T08:39:27.460 回答
0

除了您拥有的额外括号之外,错误的to_Date()It 对我来说似乎是有效的。

即你试过这个?

CREATE MATERIALIZED VIEW TC.employee_access 
(
EMPL_ID,
EFFECTIVE_DATE,
EFFECTIVE_DATE_TO,
DEPARTMENT,
JOB_DESCRIPTION,
TIME_STAMP,
MODIFIED_BY,
current_flag
)
...
WITH PRIMARY KEY
AS 

SELECT C.EMPL_ID AS Empl_Id,
       C.EFFECTIVE_DT AS Effective_Date,
       DECODE (n.effective_dt,
               c.effective_dt, TO_DATE ('01/01/2099', 'DD/MM/YYYY'),
               n.effective_dt - 1)
              AS Effective_Date_To,
       C.DFLT_UDT06_ID AS Department,     
       C.DFLT_UDT08_ID AS Job_Description,
       C.TIME_STAMP,
       C.MODIFIED_BY,
       CASE WHEN c.effective_date < sysdate
                 and effective_date_to > sysdate
       THEN  'Y'
       ELSE 'N'            
       END as current_flag
  FROM empl_history_mv c, empl_history_mv n
 ....
于 2013-01-15T07:59:07.577 回答