以下代码是否有效?我收到了错误。

时间:2011-03-23 02:41:09

标签: sql oracle view ora-00928

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;
/

--------------------以下错误------------------------- --------------------------

  

在命令的第1行开始出错:创建或替换视图点AS   DECLARE avgDurationOurFault number(5)命令行错误:1   列:32错误报告:SQL错误:ORA-00928:缺少SELECT关键字   00928. 00000 - “缺少SELECT关键字”   *原因:
  *操作:

     

在命令的第4行开始出错:   avgDurationCustomersFault number(5)错误报告:未知命令

     

在命令的第5行开始出错:avgDuration number(5)错误   报告:未知命令

     

从命令行第7行开始出错:

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;

     

错误报告:ORA-06550:第3行第2列:PLS-00103:遇到   符号“(”当期待下列之一时:

     如果loop mod null pragma raise,

开始case声明退出goto   使用<<返回选择更新关闭当前删除   fetch lock insert open rollback savepoint set sql execute commit   forall merge pipe符号“更新”   取代“(”继续.ORA-06550:第3行,第37栏:   PLS-00103:当期待其中一个时遇到符号“INTO”   以下内容:

     

。 (,*%& - + / at mod rem from ||符号“。   在“I ORA-06550之前插入:第3行,第67栏:PLS-00103:   遇到符号“;”期待以下之一:

     

设定   ORA-06550:第30行,第3列:PLS-00103:遇到符号   期待下列之一的“内部”:

     

,;对于具有交叉减号开始联合的组,其中
  连   06550. 00000 - “行%s,列%s:\ n%s”   *原因:通常是PL / SQL编译错误。   *操作:

2 个答案:

答案 0 :(得分:4)

视图不能像这样使用PL / SQL。您必须将所有查询放在一起。类似于CREATE OR REPLACE VIEW POINTS AS [one huge sql statement...]

答案 1 :(得分: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

可以通过使用CASE语句根据cat_id对值进行求和来组合“y”和“z”。其他人可以用它打高尔夫球。

您的查询的问题在于您尝试使用多个不相关的SELECT语句。视图是单个SELECT语句 - 您可以使用子查询,派生表/内联视图等,但它们必须位于单个查询中,就像您在我的示例中看到的那样。您发布的内容更像是您在存储过程或函数中找到的内容。你不能使用变量,比如你的尝试方式,你不需要 - 只需要一个CROSS JOIN。

可以使用子查询因子分解(AKA WITH子句,CTE),但通常很少甚至没有性能优势。