Oracle - 重写此查询以不使用子选择的方法?

时间:2011-09-21 17:05:21

标签: sql database oracle

我正在尝试在Oracle 11.1中创建一个物化视图,它显然不支持物化视图中的嵌套选择(为什么会这样,我一直无法弄清楚)..有没有办法写这个查询可以作为物化视图吗?谢谢!

CREATE MATERIALIZED VIEW MV_Area90DayReport
  NOLOGGING
  CACHE
  BUILD IMMEDIATE 
  REFRESH FAST ON COMMIT 
  AS 
    select
      T.TASKID,
      V.PROJECTID,
      V.VERSIONID,
      T.GOLDDATE,
      P.BUSINESSLAUNCHDATE,
      V.NAME as ProjectName,
      T.NAME as TaskName,
      T.COURSECODE,
      DT.NAME as DeliveryMethod,
      T.DELIVERABLELENGTHHOUR,
      T.DELIVERABLELENGTHMINUTE,
      V.PRIORITY,
      (SELECT MIN(STARTDATE) FROM TPM_TRAININGPLAN WHERE PROJECTID=V.PROJECTID AND TRAININGPLANTYPE='prescribed') as TrainingDeliveryDate,
      (SELECT wm_concat(WORKGROUPID) FROM TPM_PROJECTWORKGROUPS WHERE PROJECTID=V.PROJECTID GROUP BY PROJECTID) as Workgroups,
    from TPM_TASK T
    inner join TPM_PROJECTVERSION V ON (V.PROJECTID = T.PROJECTID AND V.VERSIONID = T.VERSIONID)
    inner join TPM_PROJECT P ON (P.PROJECTID = T.PROJECTID)
    inner join TPM_DOCUMENTTYPE DT ON (DT.DOCUMENTTYPEID = T.DOCUMENTTYPEID);

我得到的错误是:

>[Error] Script lines: 1-25 -------------------------
 ORA-22818: subquery expressions not allowed here
 Script line 20, statement line 20, column 115 

2 个答案:

答案 0 :(得分:2)

我认为这是一个限制(在某个时候被提出作为一个错误),在Oracle网站上记录 - http://download.oracle.com/docs/cd/B12037_01/server.101/b10736/basicmv.htm#sthref431

要解决此问题,您应该使用JOINS而不是子查询。

答案 1 :(得分:1)

尝试以下查询:

select
  T.TASKID,
  V.PROJECTID,
  V.VERSIONID,
  T.GOLDDATE,
  P.BUSINESSLAUNCHDATE,
  V.NAME as ProjectName,
  T.NAME as TaskName,
  T.COURSECODE,
  DT.NAME as DeliveryMethod,
  T.DELIVERABLELENGTHHOUR,
  T.DELIVERABLELENGTHMINUTE,
  V.PRIORITY,
  TP.TrainingDeliveryDate,
  WG.Workgroups,
from TPM_TASK T
inner join TPM_PROJECTVERSION V ON (V.PROJECTID = T.PROJECTID AND V.VERSIONID = T.VERSIONID)
inner join TPM_PROJECT P ON (P.PROJECTID = T.PROJECTID)
inner join TPM_DOCUMENTTYPE DT ON (DT.DOCUMENTTYPEID = T.DOCUMENTTYPEID)
left join (
  SELECT PROJECTID, MIN(STARTDATE) as TrainingDeliveryDate
  FROM TPM_TRAININGPLAN
  WHERE TRAININGPLANTYPE='prescribed'
  GROUP BY PROJECTID
) TP on TP.PROJECTID=V.PROJECTID
left join (
  SELECT PROJECTID, wm_concat(WORKGROUPID) as Workgroups
  FROM TPM_PROJECTWORKGROUPS
  GROUP BY PROJECTID
) WG on WG.PROJECTID=V.PROJECTID