0

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。

4

1 回答 1

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.
于 2013-01-25T12:12:17.160 回答