Solved

I've got to change the date range for this select. How?

Posted on 2016-10-03
4
38 Views
Last Modified: 2016-10-04
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]

Open in new window


The part where the range of dates are being qualified is here:

having max(posted) > dbo.ufn_lastpaperstatementbefore(12159877, 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?
0
Comment
Question by:brucegust
  • 2
4 Comments
 
LVL 42

Expert Comment

by:zephyr_hex (Megan)
ID: 41827003
As a side note, there is no need to LTRIM the result of this case statement, still the values are static:

LTRIM( case when type = 'a' then 'Adjustment' when type = 'p' then 'Payment' when type = 'c' then 'Charge' end ) as txntype

Open in new window

0
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 200 total points
ID: 41827011
If you want to find posted dates greater than 45 days ago, you would do:

having max(posted) > DATEADD(DAY, -45, GETDATE())

If you want to exclude the time component, use:
DATEADD(DAY, -45, DATEDIFF(DAY, 0, GETDATE()))
0
 
LVL 42

Accepted Solution

by:
zephyr_hex (Megan) earned 300 total points
ID: 41827012
To get everything in the last 45 days, change:

max(posted) > dbo.ufn_lastpaperstatementbefore(12159877, cast('2050-01-01' as date)) )

Open in new window


to

max(posted) > DATEADD("d", -45, CAST(GETDATE() AS DATE))

Open in new window


When run for today, this:
SELECT DATEADD("d", -45, CAST(GETDATE() AS DATE))

returns 8/19/2016
0
 

Author Comment

by:brucegust
ID: 41828028
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now