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?
 
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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
What are the definition from vw_Lace_Discharge_Readmit_Days and vw_MedAssets_DRG_Tables?
0
 
GPSPOWAuthor Commented:
Thanks

Glen
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.