ORA-00918:选择最后一行时出现列模糊定义的错误

时间:2017-10-03 04:17:55

标签: sql oracle

我有一个SQL脚本来显示结果集返回的最后一行。

SELECT * FROM
(
SELECT
D.DESCRIPTIONID_FK,
D.DAY,
D.TIME,
D.PROFILEID_FK,
E.DESCRIPTIONID,
E.DESCRIPTION,
E.PROFILEID_FK,
E.TONEID_FK,
T.TONEID,
T.TONE_NAME,
T.TONE_FILE
FROM
WA_BT_TBL_DAY D,
WA_BT_TBL_DESCRIPTION E,
WA_BT_TBL_TONE T
WHERE
D.DESCRIPTIONID_FK = E.DESCRIPTIONID AND
E.TONEID_FK = T.TONEID AND
D.DAY = 'Tue' AND
D.TIME <= '11:00'
ORDER BY TIME DESC
)
WHERE ROWNUM = 1

当我查询时,它显示错误:

ORA-00918: column ambiguously defined

我正在为此目的使用TOAD。

3 个答案:

答案 0 :(得分:4)

问题在于D.PROFILEID_FKE.PROFILEID_FK,因为当您尝试使用*选择全部时,它们将具有相同的名称,请尝试以下操作:

SELECT * FROM
(
    SELECT
        D.DESCRIPTIONID_FK,
        D.DAY,
        D.TIME,
        D.PROFILEID_FK as D_PROFILEID, -- HERE IS WHAT WRONG
        E.DESCRIPTIONID,
        E.DESCRIPTION,
        E.PROFILEID_FK as E_PROFILEID, --AND HERE
        E.TONEID_FK,
        T.TONEID,
        T.TONE_NAME,
        T.TONE_FILE,
        row_number() over(order by d.time desc) as rn
    FROM
        WA_BT_TBL_DAY D,
        WA_BT_TBL_DESCRIPTION E,
        WA_BT_TBL_TONE T
    WHERE
        D.DESCRIPTIONID_FK = E.DESCRIPTIONID AND
        E.TONEID_FK = T.TONEID AND
        D.DAY = 'Tue' AND
        D.TIME <= '11:00'
) 
WHERE rn= 1

答案 1 :(得分:1)

问题在于以下列

.
.
.
D.PROFILEID_FK,
.
.
E.PROFILEID_FK,
.
.
.

如果为这些列指定了不同的别名,则问题将得到解决。

D.PROFILEID_FK d_profileid_fk

E.PROFILEID_FK e_profileid_fk

答案 2 :(得分:0)

唯一的列TIME未使用它的表别名定义。它应该是

ORDER BY D.TIME DESC