Here is the SELECT statement as it looks currently. Don't be too put off by it's "size." We're only looking at that place where the range of dates are being qualified. Here's the code:
select LTRIM( case when type = 'a' then 'Adjustment' when type = 'p' then 'Payment' when type = 'c' then 'Charge' end ) as txntype, [date], patientname, description, amount, physicianid, practclaimid, priority, cpt, case when practclaimid = '' then 1 else 0 end as toppriority, balance, firstdos from ( select paymentmethod, payerdesc, type, [date] = case when type = 'c' then dos else posted end, pfirst+' '+plast as patientname, LTRIM( case when type = 'a' then payerdesc when type = 'p' then payerdesc when type = 'c' then ISNULL(cpt, '') +' - '+ ISNULL(left(cptdesc,100),'') end ) as description, amount,'Barton County Memorial Hospital' as physicianid, coalesce(cg.encountercode, '') as practclaimid, case when type = 'c' then 0 else 1 end as priority, left(cptdesc,100) as cptdesc, cpt as cpt, (select sum(amount) from txn where t.chargegroupid = txn.chargegroupid and t.accountid = txn.accountid) as balance, (select min(dos) from txn where t.chargegroupid = txn.chargegroupid and t.accountid = txn.accountid and type = 'c') as firstdos from txn t left join chargegroup cg on t.chargegroupid = cg.id where t.accountid = 12159877 and (cg.status1 not in ('rt','hd','rp','fc') or cg.status1 is null) and cg.id in ( select chargegroupid from txn where accountid = 12159877 group by chargegroupid having max(posted) > dbo.ufn_lastpaperstatementbefore(12159877, cast('2050-01-01' as date)) ) and reversedref is null ) u order by firstdos, practclaimid, priority,[date]
The part where the range of dates are being qualified is here:
having max(posted) > dbo.ufn_lastpaperstatement
59877, cast('2050-01-01' as date)) )
The formula returns the date of the most recent statement. Everything since then is now being included in the recordset.
I need to change that. Rather than everything since the last statement, I need everything that's been posted within the last 45 days.
In another query, I came across this:
having datediff(d, max(t1.created), getDate()) <= 45
The utility of that code looks exactly like what I need.
How do I incorporate that into the original SELECT statement?