我在oracle SqlDevelopper中的这个关联查询中做错了什么

时间:2017-04-07 16:39:21

标签: sql oracle oracle-sqldeveloper correlation

我试图建立一个查询来选择相关系数,以确定用户评分最高的电影

我桌子的结构是这样的:

enter image description here

我的疑问是:

SELECT  
    user1, user2,
    ((psum - (sum1 * sum2 / n)) / sqrt((sum1sq - pow(sum1, 2.0) / n) * (sum2sq - pow(sum2, 2.0) / n))) AS r,
    n
FROM
    (SELECT 
         n1.idclient AS user1,
         n2.idclient AS user2,
         SUM(n1.cote) AS sum1,
         SUM(n2.cote) AS sum2,
         SUM(n1.cote * n1.cote) AS sum1sq,
         SUM(n2.cote * n2.cote) AS sum2sq,
         SUM(n1.cote * n2.cote) AS psum,
         COUNT(*) AS n
     FROM
         cote AS n1 // <---------- Editor is pointing a missing parenthese here with a red line 
    LEFT JOIN
        cote AS n2 ON n1.idfilm = n2.idfilm
    WHERE   
        n1.idclient > n2.idclient
    GROUP BY
        n1.idclient, n2.idclient) AS step1
ORDER BY
    r DESC, n DESC

但我确信我没有错过括号,我做错了什么?

P.S:我不是专家,我想了解

1 个答案:

答案 0 :(得分:2)

Oracle不允许您将AS用于表别名,仅用于列别名。 The select-list syntax显示可选的AS; the table-reference syntax没有。

在您突出显示的行中

FROM cote AS n1

AS视为表别名(尽管它是一个保留字,因此您不能将其用作别名);然后它看到n1并且不知道它意味着什么。在这种情况下,解析器似乎经常回到猜测任何打开的括号应该在n1令牌之前关闭,因为它可以完成它到目前为止解析的内容(我非常简化,当然);因此不是完全有用的错误信息。

因此,您需要从正在使用它的所有三个地方删除AS

SELECT  
        user1, user2,
        ((psum - (sum1 * sum2 / n)) / sqrt((sum1sq - pow(sum1, 2.0) / n) * (sum2sq - pow(sum2, 2.0) / n))) AS r,
        n
FROM
        (SELECT 
                n1.idclient AS user1,
                n2.idclient AS user2,
                SUM(n1.cote) AS sum1,
                SUM(n2.cote) AS sum2,
                SUM(n1.cote * n1.cote) AS sum1sq,
                SUM(n2.cote * n2.cote) AS sum2sq,
                SUM(n1.cote * n2.cote) AS psum,
                COUNT(*) AS n
        FROM
                cote n1
    LEFT JOIN
        cote n2
    ON
        n1.idfilm = n2.idfilm
        WHERE   
                n1.idclient > n2.idclient
    GROUP BY
        n1.idclient, n2.idclient) step1
ORDER BY
        r DESC,
        n DESC