Skip to content
Advertisement

How to accurately pull minimum date of each time a payment method was changed

I need to get the minimum date for each time a payment method changes between the method id, account number, or routing number. I’m bouncing between a cursor and between windows functions. Doing the method below seems to work fine, but when i use this method with a larger dataset, with different pay ids involved, it doesn’t seem to be doing things in order by person so some will have duplicate entrys.

Duplicates would be 2 payments of the same kind within the same date range of being used. I only want the min date.

With windows functions this is what i’ve tried so far. I’m looking for suggestions on how to make this more accurate for larger datasets. Do i even go this route, or do i turn to using a stored procedure that takes in one person at a time?

Desired result would be

Advertisement

Answer

I think you can use lag() to get the first record after things change:

Here is a db<>fiddle.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement