0

I have only gone so far:

select timestamp, trans, Count(trans)  
From(
    Select to_char(CREATED_TIMESTAMP) as timestamp,SOURCE_MSISDN||DEST_MSISDN||AMOUNT as trans
    From ADMDBMC.TRANSACTION_CASH
    WHERE TO_date(CREATED_TIMESTAMP) > = '1-sep-2014' AND TO_date(CREATED_TIMESTAMP) < '2-sep-2014'
    and STATUS_DESCRIPTION='SUCCESS'
    ) 

group by timestamp,trans
Having count(trans)>1
order by count(trans) desc
Erik Kaplun
  • 35,169
  • 14
  • 97
  • 106
Kax
  • 3
  • 2

3 Answers3

0

I think you can achieve that by truncating your timestamp on the nearest multiple of 10 minutes: you could replace to_char(CREATED_TIMESTAMP) as timestamp with regexp_replace(to_char(CREATED_TIMESTAMP, 'dd-Mon-yyyy hh24:mi'), '.$', '0') and I think your group by would then be ok.

EDIT : the previous solution was only working if the 2 transactions were part of the same multiple of 10 minutes. Here is a better one:

Select *
From
(
    Select CREATED_TIMESTAMP,
           SOURCE_MSISDN||DEST_MSISDN||AMOUNT as trans,
           lag(CREATED_TIMESTAMP, 1, null) over (partition by SOURCE_MSISDN||DEST_MSISDN||AMOUNT order by CREATED_TIMESTAMP) as PREVIOUS_TIMESTAMP
    From ADMDBMC.TRANSACTION_CASH
    Where TO_date(CREATED_TIMESTAMP) >= '1-sep-2014'
      And TO_date(CREATED_TIMESTAMP) < '2-sep-2014'
      And STATUS_DESCRIPTION='SUCCESS'
) 
where CREATED_TIMESTAMP <= PREVIOUS_TIMESTAMP + INTERVAL '10' MINUTE

The lag analytical function allows to get for every row the timestamp of preceeding row of the same group identified by the concatenation of SOURCE_MSISDN / DEST_MSISDN / AMOUNT. Then once we have this, we just have to check the difference.

Emmanuel
  • 13,267
  • 11
  • 46
  • 70
  • I want to check the time in between time stamps.. if the time is less than 10 min then return the result.. so for example if first time time stamp is 1:03 AM and next time stamp is 1:12 AM then then that transaction should appear in the resuts – Kax Sep 10 '14 at 12:05
0

Emmanuels way would return false negative for example if one transaction occured at 11:49 and second at 11:52

You could use LEAD or LAG aggregate function with window option to get timestamp of a older row then current and calculate difference between two timestamps or COUNT(*) with window, for example:

select * FROM ( Select to_char(CREATED_TIMESTAMP) as timestamp,SOURCE_MSISDN||DEST_MSISDN||AMOUNT as trans ,COUNT(*) OVER (PARTITION BY SOURCE_MSISDN||DEST_MSISDN||AMOUNT ORDER BY CREATED_TIMESTAMP RANGE NUMTODSINTERVAL(600,'SECOND') PRECEDING) L_OF_TRANS From ADMDBMC.TRANSACTION_CASH WHERE TO_date(CREATED_TIMESTAMP) > = '1-sep-2014' AND TO_date(CREATED_TIMESTAMP) < '2-sep-2014' and STATUS_DESCRIPTION='SUCCESS' ) WHERE L_OF_TRANS > 1

Community
  • 1
  • 1
0

You should try using to_dsinterval function. Subtracting timestamps gives intervals, so you can compare timestamps using to_dsinterval. For example:

select *
from my_table x
where x.timestamp1 - x.timestamp2 < to_dsinterval ('00 00:10:00');

In this example, timestamp1 would be the more recent timestamp.

tbone
  • 14,580
  • 3
  • 32
  • 40