SQL ORA-01791: 不是 SELECTed 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31691470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:51:46  来源:igfitidea点击:

ORA-01791: not a SELECTed expression

sqloracle

提问by drup

I need to fetch details from DB. Any thing wrong in my code?

我需要从数据库中获取详细信息。我的代码有什么问题吗?

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

This giving me an error:

这给了我一个错误:

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

回答by Shishir Kumar

The problem here is the ORDER BYcolumn TITLEisn't selected in the DISTINCTquery. Since DISTINCTis used, the SELECTquery will try to group the resultsetbased on the selected columns.

这里的问题是未在查询中选择该ORDER BY列。由于使用了,查询将尝试根据选定的列对进行分组。TITLEDISTINCTDISTINCTSELECTresultset

ORDER BYcolumn isn't selected here, it doesn't ensure the uniqueness on the resultsetand hence it fails to apply ORDER BY.

ORDER BY此处未选择列,它不能确保 上的唯一性resultset,因此无法应用ORDER BY

回答by Piyushkumar Kachhadiya

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
)