我使用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
任何想法如何获得预期的排序结果?
先感谢您。
最佳答案
您可以先对它们进行排序,然后选择
select name, created
from
(select reportname as name, convert_to_char(a.date) AS created, a.date,
....
from table1
UNION
select reportname as name, convert_to_char(a.date) AS created, a.date
....
from table2
UNION
select reportname as name, convert_to_char(a.date) AS created, a.date
....
from table3
order by a.date desc);
关于sql - oracle:按日期和时间戳排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43469932/