Thanks for looking.
I am trying to get the smallest time between visits per customer. With the below code I am getting the amount of time between each visit, but it is comparing every date. I just need the smallest margin and then to get rid of/hide all the others.
select a.account_id, a.transaction_id, b.transaction_date , a.transaction_date, round(a.transaction_date - b.transaction_date, 0) as Time_between
from time_between_trans a, time_between_trans b
where a.transaction_date > b.transaction_date
and a.account_ID like '717724'
and a.account_id = b.account_id
-
for each date in transaction_date find the closest date to it in the
trasactions -
They must be of the same account_id and the second date must be later
than the first
You need to add a group by to your query:
I also fixed your join syntax to use proper join syntax, and changed the “like” to an “=”, since you have no wildcards.
There may be other methods to express this, but this is standard SQL. You should specify the database you are using in your question.
If you are using Oracle, then just do the following:
Actually, this isn’t quite correct, because you have to take NULLs into account for nexttd and prevtd. But, the idea is simple. The lead and lag functions let you take the prev or text transaction, and then you can do whatever you want to find the minimum — or get whatever other information you would like from the records.