ORA-01791:不是SELECTed表达式

时间:2015-07-29 04:54:40

标签: sql oracle

我需要从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

2 个答案:

答案 0 :(得分:57)

此处的问题是在ORDER BY查询中未选择TITLEDISTINCT。由于使用了DISTINCTSELECT查询会尝试根据所选列对resultset进行分组。

此处未选择

ORDER BY列,它不会确保resultset的唯一性,因此无法应用ORDER BY

答案 1 :(得分:-1)

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
)