我选择使用union
三个表的一些数据,那么我想按日期和时间戳对它们进行排序(递减):预言:按日期排序和时间戳
select reportname as name, convert_to_char(a.date) AS created,
....
from table1
UNION
select reportname as name, convert_to_char(a.date) AS created,
....
from table2
UNION
select reportname as name, convert_to_char(a.date) AS created,
....
from table3
order by created desc;
convert_to_char实现如下:
create or replace function convert_to_char(myDate date)
RETURN varchar IS
BEGIN
return TO_CHAR(cast (myDate as timestamp) at local, 'YYYY-MM-DD HH24:MI:SS TZH:TZM');
END;
排序的数据我得到的结果是这样的:
created
------------------
2017-04-12 16:07:07 +02:00
2017-04-13 09:00:01 +02:00
2017-04-13 09:00:40 +02:00
2017-04-12 16:06:17 +02:00
2017-04-12 16:08:37 +02:00
2017-04-12 16:07:31 +02:00
2017-04-13 09:00:25 +02:00
2017-04-13 09:00:25 +02:00
2017-04-12 16:09:07 +02:00
2017-04-12 16:08:20 +02:00
2017-04-12 16:08:06 +02:00
2017-04-12 16:06:48 +02:00
但我期待以下排序结果:
created
------------------
2017-04-13 09:00:40 +02:00
2017-04-13 09:00:25 +02:00
2017-04-13 09:00:25 +02:00
2017-04-13 09:00:01 +02:00
2017-04-12 16:09:07 +02:00
2017-04-12 16:08:37 +02:00
2017-04-12 16:08:20 +02:00
2017-04-12 16:08:06 +02:00
2017-04-12 16:07:31 +02:00
2017-04-12 16:07:07 +02:00
2017-04-12 16:06:48 +02:00
2017-04-12 16:06:17 +02:00
任何想法如何获得预期的排序结果?
谢谢你提前。
'sort by'?在Oracle中? –
为什么不使用'ORDER BY a.date' –
我已经更新为“order by”。排序依据是错字 –