我有一个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。
答案 0 :(得分:4)
问题在于D.PROFILEID_FK
和E.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