Oracle 中select distinct 和order by同时使用的问题

Oracle 11g数据库,执行下面语句出现错误“ORA-01791: 不是 SELECTed 表达式”:

SELECT DISTINCT PROC_INST_ID_ FROM act_hi_taskinst t1 WHERE ASSIGNEE_ = #{userId }

SELECT语句中含有DISTINCT关键字或者有运算符时,排序用字段必须与SELECT语句中的字段相对应。

如果select子句中出现了distinct关键字,则只能用出现过的列名

解决方法:

SELECT DISTINCT PROC_INST_ID_ , max(END_TIME_) as endTime FROM act_hi_taskinst t1 WHERE ASSIGNEE_ = 'U0001' group by PROC_INST_ID_ order by endTime desc

SELECT DISTINCT PROC_INST_ID_ , min(END_TIME_) as endTime FROM act_hi_taskinst t1 WHERE ASSIGNEE_ = 'U0001' group by PROC_INST_ID_ order by endTime desc

参考:

https://blog.csdn.net/zhu7478848/article/details/53190649

https://www.cnblogs.com/chenxizhaolu/p/6932289.html

你可能感兴趣的:(Oracle 中select distinct 和order by同时使用的问题)