The title may not exactly explain with this post is about. I had the same problem while trying to search for a solution to this puzzle.
Background:
I work in debt collection industry. The collectors call debtors and collect money on the debts and based on the amount of money that they collect in a given month, they earn their commission. Sometimes towards the EoM, collectors are notoriously well known to be collecting any (good/bad) payments that they can, so to inflate their monthly numbers. And many a times the bad payment check will bounce as an NSF. But each of these NSFs cost big bucks to the Collection Agencies and the Debtor.
Puzzle:
One of the new analysis required was to find out which collector(s) had more than 10 NSF payments within any 5 day range. Example, between the range of April 1 and April 5, find all collectors that had more than 10 NSF payments or between the range of April 2 and April 6, find all collectors that had more than 10 NSF payments and so on.
Solution:
Here is a nice SQL statement that I wrote that does exactly the same as required:
select a.collector, tbl.dtrange, count(0) CountOfNSFs
from (
select distinct collector, convert(varchar(10), importedon, 101) + ' - ' + convert(varchar(10), importedon+4, 101) DtRange,
convert(varchar(10), importedon, 101) start, convert(varchar(10), importedon+4, 101) stop, count(0) NSFCount
from ach_nsf
where importedon between importedon and importedon+5
group by collector, convert(varchar(10), importedon, 101) + ' - ' + convert(varchar(10), importedon+4, 101), convert(varchar(10), importedon, 101) , convert(varchar(10), importedon+4, 101)) tbl
inner join ach_nsf a
on tbl.collector = a.collector
and a.importedon between tbl.start and tbl.stop
where a.importedon between tbl.start and tbl.stop
group by a.collector, tbl.dtrange, tbl.start
having count(0) >= 10
Background:
I work in debt collection industry. The collectors call debtors and collect money on the debts and based on the amount of money that they collect in a given month, they earn their commission. Sometimes towards the EoM, collectors are notoriously well known to be collecting any (good/bad) payments that they can, so to inflate their monthly numbers. And many a times the bad payment check will bounce as an NSF. But each of these NSFs cost big bucks to the Collection Agencies and the Debtor.
Puzzle:
One of the new analysis required was to find out which collector(s) had more than 10 NSF payments within any 5 day range. Example, between the range of April 1 and April 5, find all collectors that had more than 10 NSF payments or between the range of April 2 and April 6, find all collectors that had more than 10 NSF payments and so on.
Solution:
Here is a nice SQL statement that I wrote that does exactly the same as required:
select a.collector, tbl.dtrange, count(0) CountOfNSFs
from (
select distinct collector, convert(varchar(10), importedon, 101) + ' - ' + convert(varchar(10), importedon+4, 101) DtRange,
convert(varchar(10), importedon, 101) start, convert(varchar(10), importedon+4, 101) stop, count(0) NSFCount
from ach_nsf
where importedon between importedon and importedon+5
group by collector, convert(varchar(10), importedon, 101) + ' - ' + convert(varchar(10), importedon+4, 101), convert(varchar(10), importedon, 101) , convert(varchar(10), importedon+4, 101)) tbl
inner join ach_nsf a
on tbl.collector = a.collector
and a.importedon between tbl.start and tbl.stop
where a.importedon between tbl.start and tbl.stop
group by a.collector, tbl.dtrange, tbl.start
having count(0) >= 10
Comments