ORACLE SQL: Select 条时差小于一分钟的记录
ORACLE SQL: Select records with time difference less than a minute
我有一个 table,其中我有相同的记录,但时间戳不同。
例如。 - 1
2015-03-03 13:41:11.0 [createService]: [accountNumber = 123456]
2015-03-02 01:19:11.0 [createService]: [accountNumber = 123456]
2015-03-02 01:19:06.0 [createService]: [accountNumber = 123456]
例如- 2
2015-03-07 01:21:11.0 [createService]: [accountNumber = 567890]
2015-03-04 01:17:11.0 [createService]: [accountNumber = 567890]
我只需要提取任何一天的时间差小于一分钟的超过 2 行的记录。在上面的示例中,我应该只提取帐户 123456,因为它在同一天有 2 条记录,时差小于 60 秒。
这是我用来多次提取所有具有 createservice 行但需要使用上述条件进一步过滤列表的记录的查询 -
SELECT acct_number
FROM CUSTOMER_LOG
WHERE LOG_EVENT LIKE '%createService%serviceName%'
AND LOG_EVENT_TMST > to_date('03/01/2015 00:00:00','MM/DD/YYYY HH24:MI:SS')
GROUP BY ACCT_NUMBER
having count(ACCT_NUMBER) > 1;
能否请您帮我做一个最快的查询?提前致谢!
使用lag()
获取之前的时间戳。剩下的只是基本查询:
select acct_number, trunc(LOG_EVENT_TMST)
from (select cl.*, lag(log_event_tmst) over (partition by acct_number order by log_event_tmst) as prev_let
from customer_log cl
where LOG_EVENT_TMST > to_date('03/01/2015 00:00:00','MM/DD/YYYY HH24:MI:SS')
) cl
where (log_event_tmst - prevlet) < 1.0 / (60 * 24)
group by acct_number, trunc(LOG_EVENT_TMST);
SELECT *
FROM CUSTOMER_LOG c
WHERE LOG_EVENT LIKE '%createService%serviceName%'
AND LOG_EVENT_TMST > DATE '2015-01-03'
AND EXISTS ( SELECT 'X'
FROM CUSTOMER_LOG x
WHERE c.LOG_EVENT = x.LOG_EVENT
AND c.ACCT_NUMBER = x.ACCT_NUMBER
AND c.LOG_EVENT_TMST
BETWEEN x.LOG_EVENT_TMST - INTERVAL '1' MINUTE
AND x.LOG_EVENT_TMST + INTERVAL '1' MINUTE
);
或
SELECT *
FROM CUSTOMER_LOG
WHERE LOG_EVENT LIKE '%createService%serviceName%'
AND LOG_EVENT_TMST > DATE '2015-01-03'
AND ( LAG( LOG_EVENT_TMST ) OVER ( PARTITION BY LOG_EVENT, ACCT_NUMBER ORDER BY LOG_EVENT_TMST )
> LOG_EVENT_TMST - INTERVAL '1' MINUTE
OR LEAD( LOG_EVENT_TMST ) OVER ( PARTITION BY LOG_EVENT, ACCT_NUMBER ORDER BY LOG_EVENT_TMST )
< LOG_EVENT_TMST + INTERVAL '1' MINUTE
);
我有一个 table,其中我有相同的记录,但时间戳不同。 例如。 - 1
2015-03-03 13:41:11.0 [createService]: [accountNumber = 123456]
2015-03-02 01:19:11.0 [createService]: [accountNumber = 123456]
2015-03-02 01:19:06.0 [createService]: [accountNumber = 123456]
例如- 2
2015-03-07 01:21:11.0 [createService]: [accountNumber = 567890]
2015-03-04 01:17:11.0 [createService]: [accountNumber = 567890]
我只需要提取任何一天的时间差小于一分钟的超过 2 行的记录。在上面的示例中,我应该只提取帐户 123456,因为它在同一天有 2 条记录,时差小于 60 秒。
这是我用来多次提取所有具有 createservice 行但需要使用上述条件进一步过滤列表的记录的查询 -
SELECT acct_number
FROM CUSTOMER_LOG
WHERE LOG_EVENT LIKE '%createService%serviceName%'
AND LOG_EVENT_TMST > to_date('03/01/2015 00:00:00','MM/DD/YYYY HH24:MI:SS')
GROUP BY ACCT_NUMBER
having count(ACCT_NUMBER) > 1;
能否请您帮我做一个最快的查询?提前致谢!
使用lag()
获取之前的时间戳。剩下的只是基本查询:
select acct_number, trunc(LOG_EVENT_TMST)
from (select cl.*, lag(log_event_tmst) over (partition by acct_number order by log_event_tmst) as prev_let
from customer_log cl
where LOG_EVENT_TMST > to_date('03/01/2015 00:00:00','MM/DD/YYYY HH24:MI:SS')
) cl
where (log_event_tmst - prevlet) < 1.0 / (60 * 24)
group by acct_number, trunc(LOG_EVENT_TMST);
SELECT *
FROM CUSTOMER_LOG c
WHERE LOG_EVENT LIKE '%createService%serviceName%'
AND LOG_EVENT_TMST > DATE '2015-01-03'
AND EXISTS ( SELECT 'X'
FROM CUSTOMER_LOG x
WHERE c.LOG_EVENT = x.LOG_EVENT
AND c.ACCT_NUMBER = x.ACCT_NUMBER
AND c.LOG_EVENT_TMST
BETWEEN x.LOG_EVENT_TMST - INTERVAL '1' MINUTE
AND x.LOG_EVENT_TMST + INTERVAL '1' MINUTE
);
或
SELECT *
FROM CUSTOMER_LOG
WHERE LOG_EVENT LIKE '%createService%serviceName%'
AND LOG_EVENT_TMST > DATE '2015-01-03'
AND ( LAG( LOG_EVENT_TMST ) OVER ( PARTITION BY LOG_EVENT, ACCT_NUMBER ORDER BY LOG_EVENT_TMST )
> LOG_EVENT_TMST - INTERVAL '1' MINUTE
OR LEAD( LOG_EVENT_TMST ) OVER ( PARTITION BY LOG_EVENT, ACCT_NUMBER ORDER BY LOG_EVENT_TMST )
< LOG_EVENT_TMST + INTERVAL '1' MINUTE
);