0

输入此查询时出现此错误:

SELECT E.event, 
       ED.location, 
       ED.doe, 
       ED.category, 
       ( ( ED.numofdataset / ED.numoftimesloc ) * 100 )                    AS 
       LOGLOC, 
       ( ( ED.numofdataset / ED.numoftimesdoe ) * 100 )                    AS 
       LOGDOE, 
       ( ( ED.numofdataset / ED.numoftimescat ) * 100 )                    AS 
       LOGCAT, 
       (( ( ED.numoftimesloc * ( Log(logloc) ) ) + 
           ( ED.numoftimesdoe * ( Log(logdoe) ) ) + ( ED.numoftimescat * 
                                                      ( Log(logcat) ) ) )) AS 
       FINALRANK 
FROM   event E, 
       eventdetail ED 
WHERE  E.eventid = ED.eventid 
       AND ED.location = 'LONDON' 
       AND ED.category = 'Sports' 
ORDER  BY finalrank DESC; 

命令控制器告诉我“LOGLOC”是“字段列表”中的未知列。谁能告诉我我做错了什么以及我对 LOG() 函数的使用是否正确?

4

2 回答 2

2

You can't perform calculations on an aliased column.

Try this:

SELECT f.event, 
       f.location, 
       f.doe, 
       f.category, 
       f.logloc, 
       f.logdoe, 
       f.logcat, 
       (( ( f.numoftimesloc * ( Log(f.logloc) ) ) + 
             ( f.numoftimesdoe * ( Log(f.logdoe) ) ) + ( 
           f.numoftimescat * ( Log(f.logcat) ) ) )) AS FINALRANK 
FROM   (SELECT E.event, 
               ED.location, 
               ED.doe, 
               ED.category, 
               ( ( ED.numofdataset / ED.numoftimesloc ) * 100 ) AS LOGLOC, 
               ( ( ED.numofdataset / ED.numoftimesdoe ) * 100 ) AS LOGDOE, 
               ( ( ED.numofdataset / ED.numoftimescat ) * 100 ) AS LOGCAT, 
               ed.numoftimesloc, 
               ed.numoftimesdoe, 
               ed.numoftimescat 
        FROM   event E, 
               eventdetail ED 
        WHERE  E.eventid = ED.eventid 
               AND ED.location = 'LONDON' 
               AND ED.category = 'Sports' 
        ORDER  BY finalrank DESC) f 
于 2013-03-06T18:52:06.590 回答
0

它不是已知列,因为它不在桌面上。您在此处为其创建别名:

((ED.numofdataset / ED.numoftimesloc) * 100) AS LOGLOC

但是,不能在SELECT子句的其他地方引用别名列。(认为​​它们直到在该子句被解析之后才真正存在。因此它们可以用于某些其他子句,例如ORDER BY, 因为它对结果进行操作,SELECTSELECT对表进行操作.)

可能有一些我不知道的 SQL 技巧来减少代码重复,否则您可能只需要直接运行计算:

Log((ED.numofdataset / ED.numoftimesloc) * 100)
于 2013-03-06T18:54:49.210 回答