2011-03-23 2 views
0
CREATE OR REPLACE VIEW POINTS AS 
DECLARE 
    avgDurationOurFault  number(5); 
    avgDurationCustomersFault number(5); 
    avgDuration   number(5); 

BEGIN 

    (select ceil(avg(abs(total_time))) into avgDuration from inquiry); 

    select ceil(avg(total_duration)) into avgDurationOurFault 
    from 
    (
     select customer_no, sum(abs(total_time)) total_duration 
     from inquiry 
     where cat_id in ('C900', 'C901', 'C902', 'C905', 'C907', 'C908', 'C909') 
     GROUP BY customer_no); 

    select ceil(avg(total_duration)) into avgDurationCustomersFault 
    from 
    (
     select customer_no, sum(abs(total_time)) total_duration 
     from inquiry 
     where cat_id in ('C903','C904', 'C906') 
     group by customer_no); 

    select t1.customer_no, t1.callPoints, t1.durationPoints, t2.catgPoints, 
      t1.callPoints+t1.durationPoints+t2.catgPoints as totalPoints 
    from 
    (
     select customer_no, count(inquiry_id)*avgDuration callPoints , sum(abs(total_time)) durationPoints 
     from inquiry 
     group by customer_no 
     ) t1 

     inner join (

     select customer_no, sum(points) catgPoints 
     from 
     (
     select customer_no, 
      case 
       when cat_id in ('C903','C904', 'C906') 
       then 0 

      when cat_id in ('C900', 'C901', 'C902', 'C905', 'C907', 'C908', 'C909') 
       then 2*avgDuration + abs(avgDurationCustomersFault - avgDurationOurFault) 

      else 
       0 

      end as points 
      from inquiry 
      ) 
      group by customer_no 
      ) t2 

      on t1.customer_no = t2.customer_no; 


END; 
/

------------------ 아래의 오류 ----------------------- ----------------------------다음 코드가 유효합니까? 오류가 발생했습니다.

Error starting at line 1 in command: CREATE OR REPLACE VIEW POINTS AS DECLARE avgDurationOurFault number(5) Error at Command Line:1 Column:32 Error report: SQL Error: ORA-00928: missing SELECT keyword 00928. 00000 - "missing SELECT keyword" *Cause:
*Action:

Error starting at line 4 in command: avgDurationCustomersFault number(5) Error report: Unknown Command

Error starting at line 5 in command: avgDuration number(5) Error report: Unknown Command

Error starting at line 7 in command:

BEGIN 

(select ceil(avg(abs(total_time))) into avgDuration from inquiry); 

select ceil(avg(total_duration)) into avgDurationOurFault 
from 
(
    select customer_no, sum(abs(total_time)) total_duration 
    from inquiry 
    where cat_id in ('C900', 'C901', 'C902', 'C905', 'C907', 'C908', 'C909') 
    GROUP BY customer_no); 

select ceil(avg(total_duration)) into avgDurationCustomersFault 
from 
(
    select customer_no, sum(abs(total_time)) total_duration 
    from inquiry 
    where cat_id in ('C903','C904', 'C906') 
    group by customer_no); 
select t1.customer_no, t1.callPoints, t1.durationPoints, t2.catgPoints, 
     t1.callPoints+t1.durationPoints+t2.catgPoints as totalPoints 
from 
(
    select customer_no, count(inquiry_id)*avgDuration callPoints , sum(abs(total_time)) durationPoints 
    from inquiry 
    group by customer_no 
    ) t1 

    inner join (
    select customer_no, sum(points) catgPoints 
    from 
    (
    select customer_no, 
     case 
      when cat_id in ('C903','C904', 'C906') 
      then 0 

     when cat_id in ('C900', 'C901', 'C902', 'C905', 'C907', 'C908', 'C909') 
      then 2*avgDuration + abs(avgDurationCustomersFault - avgDurationOurFault) 
     else 
      0 

     end as points 
     from inquiry 
     ) 
     group by customer_no 
     ) t2 

     on t1.customer_no = t2.customer_no; 

END;

Error report: ORA-06550: line 3, column 2: PLS-00103: Encountered the symbol "(" when expecting one of the following:

begin case declare exit for goto if loop mod null pragma raise return select update while with << close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe The symbol "update" was substituted for "(" to continue. ORA-06550: line 3, column 37: PLS-00103: Encountered the symbol "INTO" when expecting one of the following:

. (, * % & - +/at mod rem as from || The symbol ". was inserted before "I ORA-06550: line 3, column 67: PLS-00103: Encountered the symbol ";" when expecting one of the following:

set ORA-06550: line 30, column 3: PLS-00103: Encountered the symbol "INNER" when expecting one of the following:

, ; for group having intersect minus order start union where
connect 06550. 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error. *Action:

+1

오류를 게시하십시오. – AndyG

+0

"오류가 발생했습니다. 제발 도와주세요!" "내 차가 작동하지 않습니다. 어떻게 해결할 수 있습니까?" 도움이 필요하면 더 많은 정보를 제공해야합니다. * 특정 * 오류 메시지로 시작하십시오. –

답변

4

사용 :

CREATE OR REPLACE VIEW POINTS AS 
SELECT a.customer_no, 
     a.callPoints, 
     a.durationPoints, 
     a.catgPoints, 
     a.callPoints + a.durationPoints + a.catgPoints as totalPoints 
    FROM (SELECT i.customer_no, 
       COUNT(i.inquiry_id) * x.avgDuration AS callPoints, 
       SUM(ABS(i.total_time)) durationPoints, 
       SUM(CASE 
        WHEN i.cat_id IN ('C900', 'C901', 'C902', 'C905', 'C907', 'C908', 'C909') THEN 
         2 * x.avgDuration + ABS(z.avgDurationCustomersFault - y.avgDurationOurFault) 
        ELSE 0 
        END) AS catgpoints 
      FROM INQUIRY i 
    CROSS JOIN (SELECT CEIL(AVG(ABS(t.total_time))) AS avgDuration 
        FROM INQUIRY t) x 
    CROSS JOIN (SELECT CEIL(AVG(total_duration)) AS avgDurationOurFault 
        FROM (SELECT SUM(ABS(t.total_time)) AS total_duration 
          FROM INQUIRY t 
         WHERE t.cat_id IN ('C900', 'C901', 'C902', 'C905', 'C907', 'C908', 'C909') 
         GROUP BY t.customer_no) y 
    CROSS JOIN (SELECT CEIL(AVG(total_duration)) AS avgDurationCustomersFault 
        FROM (SELECT SUM(ABS(t.total_time)) AS total_duration 
          FROM INQUIRY 
         WHERE t.cat_id IN ('C903','C904', 'C906') 
         GROUP BY t.customer_no) z 
     GROUP BY i.customer_no) a 

cat_id에 따른 값을 합산하는 CASE 문을 사용하여 "Y"및 "Z"를 결합 할 수있다. 다른 사람이 골프를 칠 수 있습니다.

검색어와 관련이없는 여러 개의 관련없는 SELECT 문을 사용하려고했습니다. 뷰는 단일 SELECT 문입니다. 하위 쿼리, 파생 테이블/인라인 뷰 등을 사용할 수 있지만 필자의 예제처럼 단일 쿼리 내부에 있어야합니다. 게시 한 내용은 저장 프로 시저 또는 함수에서 찾을 수있는 것과 비슷합니다. 시도하고 있던 방법과 같은 변수를 사용할 수 없으며 꼭 필요하지도 않습니다. 교차 결합이 필요했습니다.

하위 쿼리 인수 분해 (AKA WITH 절, CTE)를 사용할 수 있지만 일반적으로 성능상의 이점은 거의 없으며 전혀 없습니다.

4

뷰는 PL/SQL을 사용할 수 없습니다 그런 식으로. 모든 쿼리를 정리해야합니다. CREATE OR REPLACE VIEW POINTS AS [one huge sql statement...]과 같은 것입니다.

+2

서브 쿼리를 인수 분해하여 해당 변수를 대체하십시오. http://www.oracle-developer.net/display.php?id=212 –