time_created和time_ended是VARCHAR字段。
SQL语句
SELECT time_created,
time_ended,
TO_TIMESTAMP (time_ended, 'YYYY/MM/DD-HH24:MI:SS:FF9')
- TO_TIMESTAMP (time_created, 'YYYY/MM/DD-HH24:MI:SS:FF9')
FROM trans
输出
2012/10/28-18:46:13.855 2012/10/28-18:47:43.357 +00 00:01:29.502000
2012/10/20-22:40:10.363 2012/10/20-22:40:35.265 +00 00:00:24.902000
2012/10/20-22:40:08.951 2012/10/20-22:40:24.717 +00 00:00:15.766000
2012/10/20-22:40:09.454 2012/10/20-22:40:28.217 +00 00:00:18.763000
2012/10/20-22:40:09.912 2012/10/20-22:40:31.767 +00 00:00:21.855000
2012/10/22-10:11:29.360 2012/10/22-10:14:08.692 +00 00:02:39.332000
2012/10/22-10:11:08.335 2012/10/22-10:11:43.781 +00 00:00:35.446000
2012/10/20-22:40:07.900 2012/10/20-22:40:17.508 +00 00:00:09.608000
2012/10/20-22:40:08.469 2012/10/20-22:40:21.144 +00 00:00:12.675000
2012/10/22-11:00:42.355 2012/10/22-11:01:41.706 +00 00:00:59.351000
2012/10/22-10:11:09.268 2012/10/22-10:11:54.185 +00 00:00:44.917000
2012/10/22-10:11:13.072 2012/10/22-10:12:21.365 +00 00:01:08.293000
我希望where子句只显示超过1分钟的记录。
我在Sun Solaris上使用Oracle数据库10g。
答案 0 :(得分:2)
如果别名为差异列,则可以将其包装在外部选择中,只需添加where
子句:
SELECT * FROM (
SELECT time_created,
time_ended,
TO_TIMESTAMP (time_ended, 'YYYY/MM/DD-HH24:MI:SS:FF9')
- TO_TIMESTAMP (time_created, 'YYYY/MM/DD-HH24:MI:SS:FF9') AS diff
FROM trans
)
WHERE diff > INTERVAL '1' MINUTE
使用与您的输出匹配的虚拟数据,填写在CTE中:
with trans as (
select '2012/10/28-18:46:13.855' time_created,
'2012/10/28-18:47:43.357' time_ended from dual
union all select '2012/10/20-22:40:10.363', '2012/10/20-22:40:35.265' from dual
union all select '2012/10/20-22:40:08.951', '2012/10/20-22:40:24.717' from dual
union all select '2012/10/20-22:40:09.454', '2012/10/20-22:40:28.217' from dual
union all select '2012/10/20-22:40:09.912', '2012/10/20-22:40:31.767' from dual
union all select '2012/10/22-10:11:29.360', '2012/10/22-10:14:08.692' from dual
union all select '2012/10/22-10:11:08.335', '2012/10/22-10:11:43.781' from dual
union all select '2012/10/20-22:40:07.900', '2012/10/20-22:40:17.508' from dual
union all select '2012/10/20-22:40:08.469', '2012/10/20-22:40:21.144' from dual
union all select '2012/10/22-11:00:42.355', '2012/10/22-11:01:41.706' from dual
union all select '2012/10/22-10:11:09.268', '2012/10/22-10:11:54.185' from dual
union all select '2012/10/22-10:11:13.072', '2012/10/22-10:12:21.365' from dual
)
SELECT * FROM (
SELECT time_created,
time_ended,
TO_TIMESTAMP (time_ended, 'YYYY/MM/DD-HH24:MI:SS:FF9')
- TO_TIMESTAMP (time_created, 'YYYY/MM/DD-HH24:MI:SS:FF9') AS diff
FROM trans
)
WHERE diff > INTERVAL '1' MINUTE
TIME_CREATED TIME_ENDED DIFF
----------------------- ----------------------- ------------------------------
2012/10/28-18:46:13.855 2012/10/28-18:47:43.357 +000000000 00:01:29.502000000
2012/10/22-10:11:29.360 2012/10/22-10:14:08.692 +000000000 00:02:39.332000000
2012/10/22-10:11:13.072 2012/10/22-10:12:21.365 +000000000 00:01:08.293000000
3 rows selected.