oracle ORA-01791:不是SELECTed表达式

taor4pac  于 2023-05-28  发布在  Oracle
关注(0)|答案(3)|浏览(183)

我需要从DB获取详细信息。我的代码有错吗?

SELECT DISTINCT FNAME, LNAME, MEMBERORG, DAYCOUNT, TIMESTAMP, COUNT(FNAME) AS total,(SELECT COUNT(*) FROM REPORT_VIEW_PAGE) AS tot
FROM REPORT_VIEW_PAGE 
WHERE ID = '68' AND TYPE = 'node'
GROUP BY FNAME, LNAME, MEMBERORG, DAYCOUNT, TIMESTAMP
ORDER BY TITLE ASC

这给了我一个错误:

ORA-01791: not a SELECTed expression
01791. 00000 -  "not a SELECTed expression"
*Cause:    
*Action:
Error at Line: 6 Column: 10
xtfmy6hx

xtfmy6hx1#

这里的问题是在DISTINCT查询中没有选择ORDER BYTITLE。由于使用了DISTINCT,因此SELECT查询将尝试根据所选列对resultset进行分组。
ORDER BY列在这里没有被选中,它不能确保resultset上的唯一性,因此它不能应用ORDER BY

xpcnnkqh

xpcnnkqh2#

title列添加到SELECT语句中。在使用DISTINCT时,SELECT语句中还必须包含ORDER BY中的所有列。

-- correct
SELECT DISTINCT a, b, c FROM tbl.x ORDER BY a,b;

-- incorrect
SELECT DISTINCT c FROM tbl.x ORDER BY a,b;

必须选择ab列。

v8wbuo2f

v8wbuo2f3#

SELECT DISTINCT FNAME, LNAME, MEMBERORG, DAYCOUNT, TIMESTAMP, total, tot
FROM
(
SELECT DISTINCT FNAME, LNAME, MEMBERORG, DAYCOUNT, TIMESTAMP, COUNT(FNAME) AS total,(SELECT COUNT(*) FROM REPORT_VIEW_PAGE) AS tot
FROM REPORT_VIEW_PAGE 
WHERE ID = '68' AND TYPE = 'node'
GROUP BY FNAME, LNAME, MEMBERORG, DAYCOUNT, TIMESTAMP
ORDER BY TITLE ASC
)

相关问题