Oracle内联视图查询

时间:2013-01-21 09:51:52

标签: sql oracle

此查询的注释(/ * * /)部分执行但整个查询在/////// part.Please hlep中遇到问题以解决此问题。

 SELECT *
    FROM (

 /*   (SELECT U1.EMAIL, S1.GRNNUM
       FROM RDT_USER U1,
            (SELECT O.ID, GRNNUM
               FROM RDT_ORGANIZATION O,
                    (SELECT GRNNUM, RECEPIENTCODE, ORGINATORCODE
                       FROM RDT_GOODSRECEIPTNOTE M
                      WHERE M.ACTIONSTATUS = 0
                        AND M.LATEST = 1
                        AND (SYSDATE - M.GENDATE) >= 0) S
              WHERE O.FUCODE = S.ORGINATORCODE) S1

      WHERE U1.ORGID = S1.ID)*/ A///////here am getting right paranthesis missing,

    (SELECT U.EMAIL, T1.GRNNUM
       FROM RDT_USER U,
            (SELECT O.ID, GRNNUM
               FROM RDT_ORGANIZATION O,
                    (SELECT GRNNUM, RECEPIENTCODE, ORGINATORCODE
                       FROM RDT_GOODSRECEIPTNOTE M
                      WHERE M.ACTIONSTATUS = 0
                        AND M.LATEST = 1
                        AND (SYSDATE - M.GENDATE) >= 0) T
              WHERE O.FUCODE = T.ORGINATORCODE) T1

      WHERE U.ORGID = T1.ID) B)
 WHERE A.GRNNUM = B.GRNNUM

2 个答案:

答案 0 :(得分:1)

您需要将别名A置于评论中,如下所示:

SELECT *
    FROM (

 /*   (SELECT U1.EMAIL, S1.GRNNUM
       FROM RDT_USER U1,
            (SELECT O.ID, GRNNUM
               FROM RDT_ORGANIZATION O,
                    (SELECT GRNNUM, RECEPIENTCODE, ORGINATORCODE
                       FROM RDT_GOODSRECEIPTNOTE M
                      WHERE M.ACTIONSTATUS = 0
                        AND M.LATEST = 1
                        AND (SYSDATE - M.GENDATE) >= 0) S
              WHERE O.FUCODE = S.ORGINATORCODE) S1

      WHERE U1.ORGID = S1.ID) A///////here am getting right paranthesis missing,
*/
    (SELECT U.EMAIL, T1.GRNNUM
       FROM RDT_USER U,
            (SELECT O.ID, GRNNUM
               FROM RDT_ORGANIZATION O,
                    (SELECT GRNNUM, RECEPIENTCODE, ORGINATORCODE
                       FROM RDT_GOODSRECEIPTNOTE M
                      WHERE M.ACTIONSTATUS = 0
                        AND M.LATEST = 1
                        AND (SYSDATE - M.GENDATE) >= 0) T
              WHERE O.FUCODE = T.ORGINATORCODE) T1

      WHERE U.ORGID = T1.ID) B)
 WHERE A.GRNNUM = B.GRNNUM

答案 1 :(得分:1)

这两个子查询/内联视图需要以某种方式连接。你可能想要一个交叉连接 - 用它们之间的逗号表示 - 在这种情况下,你也可以删除一组括号:

SELECT *
FROM   (SELECT U1.EMAIL, S1.GRNNUM
        FROM   RDT_USER U1,
               (SELECT O.ID, GRNNUM
                FROM   RDT_ORGANIZATION O,
                       (SELECT GRNNUM, RECEPIENTCODE, ORGINATORCODE
                        FROM   RDT_GOODSRECEIPTNOTE M
                        WHERE  M.ACTIONSTATUS = 0
                        AND    M.LATEST = 1
                        AND    (SYSDATE - M.GENDATE) >= 0) S
                WHERE  O.FUCODE = S.ORGINATORCODE) S1

        WHERE U1.ORGID = S1.ID) A,

       (SELECT U.EMAIL, T1.GRNNUM
        FROM   RDT_USER U,
               (SELECT O.ID, GRNNUM
                FROM   RDT_ORGANIZATION O,
                       (SELECT GRNNUM, RECEPIENTCODE, ORGINATORCODE
                        FROM   RDT_GOODSRECEIPTNOTE M
                        WHERE  M.ACTIONSTATUS = 0
                        AND    M.LATEST = 1
                        AND    (SYSDATE - M.GENDATE) >= 0) T
                WHERE  O.FUCODE = T.ORGINATORCODE) T1
        WHERE U.ORGID = T1.ID) B
 WHERE  A.GRNNUM = B.GRNNUM;

编辑我查看的查询越多,就会发现问题越多。

  1. 您的内嵌视图/子查询是相同的。交叉连接它们的唯一效果是增加执行时间。
  2. 你为什么要做交叉连接?内连接就足够了。
  3. 您的内部查询缺少某些列规范,您在连接的外部查询中引用这些规范。这不起作用。
  4. 查询的核心基本上就是这个逻辑:

    select grnnum,
           recepientcode,
           originatorcode
    from   rdt_goodsreceiptnote
    where  actionstatus       = 0
    and    latest             = 1
    and    sysdate - gendate >= 0;
    

    然后您与rdt_organization交叉加入,然后与rdt_user交叉加入;由于某种原因,这个混乱然后与自己交叉。使用内部联接可以大大简化

    select rdt_user.email,
           rdt_goodsreceiptnote.grnnum
    
    from   rdt_goodsreceiptnote
    
    join   rdt_organization
    on     rdt_organization.fucode = rdt_goodsreceiptnode.originatorcode
    
    join   rdt_user
    on     rdt_user.orgid = rdt_organization.id
    
    where  rdt_goodsreceiptnode.actionstatus       = 0
    and    rdt_goodsreceiptnode.latest             = 1
    and    sysdate - rdt_goodsreceiptnode.gendate >= 0;
    

    你没有理由在交叉连接中两次这样做,所以就是这样。更容易,是吗?