【问题标题】:Is the following code valid? I am getting errors.以下代码有效吗?我收到错误。
【发布时间】:2011-07-20 23:50:46
【问题描述】:
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 行开始出错:CREATE OR REPLACE VIEW POINTS 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;

结束;

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

begin case 为 goto 声明退出 if loop mod null pragma raise 在

。 ( , * % & - + / at mod rem as from || 符号"。 在“I ORA-06550:第 3 行,第 67 列:PLS-00103: 遇到符号“;”预期以下情况之一时:

设置 ORA-06550:第 30 行,第 3 列:PLS-00103:遇到符号 “INNER”当预期以下之一时:

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

【问题讨论】:

  • 请发布您遇到的错误。
  • “我遇到了错误。请帮忙!”就像说“我的车坏了。我该如何修理它?”如果您需要帮助,您需要提供更多信息。从特定错误消息开始。

标签: sql oracle view ora-00928


【解决方案1】:

用途:

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),但通常几乎没有性能优势。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    猜你喜欢
    • 2023-01-23
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 2011-10-29
    • 1970-01-01
    相关资源
    最近更新 更多