Selecting data based on effective date

I have a table of the data appended every year based on the effective dates. of the changes.  The data changes once a year at 10/1 and is assigned a new version number.  The relevant fields in this table are Code, Version, Effective Date, Amount.

The second table is a list of customers who come in and have a service performed and are charged  a fee based on the current rate in the first table.

I want to match up the correct fee from the first table based on the date of service and the code charged.  

What is the syntax to obtain the correct amount where the Code service date is between the beginning and ending effective date of the correct version the first table?

Thanks

Glen
GPSPOWAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Just for kicks and giggles, please give us a data mockup of both tables, and an example or three of what you're trying to pull off.  Hard to conceptualize with just the description here.  tia
0
GPSPOWAuthor Commented:
I have attached sample data for the Customers and Rates.  I am trying to match up the correct ALOS and GLOS for each customer based where the Customer DischDate falls within the EffectiveDate and 9/30 of the following year from the effective date  and where the Customer DRG = Rate DRG for all 'MCR'  financial class types.

The Customer DRG has no leading zeros and the Rates DRG has leading zeros.  The field has a length of 3.


Here are 3 customers and their expected outcomes:

Customer #         DRG    DischDate     ALOS      GLOS
A10000415900    357    12/22/2014      6.6          5.1
A10000420628    439     1/16/2015      8.5           6.8
A10000429519    638     3/11/2015       3.7          3


Thanks
Glen
Sample-Data.xlsx
0
GPSPOWAuthor Commented:
Here is my solution:
SELECT     VisitID, AcctNumber, MedRecNumber, AdmDate, DischDate, AdmDt2, Days, LaceDate, LaceScore, DisYear, DisMonth, MRRank, Age, DC_DispID, 
                      DC_DISPDescr, FinancialClassName, Name, RoomID, LOS, Physician, DiagnosisName, 
MaritalStatus, Ethnicity, Zip, Language, DRG,
                          (SELECT     TOP (1) ALOS
                            FROM          dbo.vw_MedAssets_DRG_Tables AS t2
                            WHERE      (CONVERT(numeric(5, 0), t1.DRG) = CONVERT(numeric(5, 0), DRG)) AND (t1.DischDate >= BegDate) AND (t1.DischDate <= EndDate)) 
                      AS ALOS,
                          (SELECT     TOP (1) GLOS
                            FROM          dbo.vw_MedAssets_DRG_Tables AS t2
                            WHERE      (CONVERT(numeric(5, 0), t1.DRG) = CONVERT(numeric(5, 0), DRG)) AND (t1.DischDate >= BegDate) AND (t1.DischDate <= EndDate)) 
                      AS GLOS,


FacNote, ApptNote, FollowUpNote, BHIPNote, BHOPNote 
                      
FROM         dbo.vw_Lace_Discharge_Readmit_Days AS t1

Open in new window


Thanks

glen
0
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

Vitor MontalvãoMSSQL Senior EngineerCommented:
What are the definition from vw_Lace_Discharge_Readmit_Days and vw_MedAssets_DRG_Tables?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Based on your sample data this should work:
SELECT c.VisitID, c.AdmDate, c.DischDate, r.EffectiveDateTime, c.DRG, r.ALOS, r.GLOS
FROM customers c
	INNER JOIN rates r ON c.DRG=r.CodeID AND r.FinancialClassID='MCR'
WHERE r.EffectiveDateTime >= CAST('10-01-'+CAST(YEAR(c.DischDate) AS CHAR(4)) AS DATE)
 AND r.EffectiveDateTime < CAST('10-01-'+CAST(YEAR(c.DischDate)+1 AS CHAR(4)) AS DATE) 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GPSPOWAuthor Commented:
Thanks

Glen
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.