我想使用oracle查询从订单表中获取唯一的日期值。我正在
ORA-01791:不是SELECTed表达式
错误,当我尝试以下查询
时SELECT DISTINCT (TO_DATE(LAST_INSERT_TIMESTAMP, 'YYYY-MM-DD HH24:MI'))
FROM ORDER
WHERE LAST_INSERT_TIMESTAMP IS NOT NULL
ORDER BY LAST_INSERT_TIMESTAMP DESC;
答案 0 :(得分:2)
LAST_INSERT_TIMESTAMP
不在您的结果列表中,因为您已使用DISTINCT
将行汇总到截断的时间戳。你只能按此订购。
SELECT DISTINCT TRUNC(LAST_INSERT_TIMESTAMP, 'MI')
FROM ORDER
WHERE LAST_INSERT_TIMESTAMP IS NOT NULL
ORDER BY TRUNC(LAST_INSERT_TIMESTAMP, 'MI') DESC;
如果您不想重复表达,请使用位置排序:
ORDER BY 1 DESC;
或者为表达式使用别名:
SELECT DISTINCT TRUNC(LAST_INSERT_TIMESTAMP, 'MI') AS LAST_INSERT
FROM ORDER
WHERE LAST_INSERT_TIMESTAMP IS NOT NULL
ORDER BY LAST_INSERT DESC;
请注意,我已将TO_DATE
替换为相应的TRUNC
,因为您要做的只是截断时间戳,而不是转换为字符串和从字符串转换。