Avatar of GPSPOW
GPSPOW
Flag for United States of America asked on

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
Microsoft SQL Server

Avatar of undefined
Last Comment
GPSPOW

8/22/2022 - Mon
Jim Horn

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
GPSPOW

ASKER
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
GPSPOW

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Vitor Montalvão

What are the definition from vw_Lace_Discharge_Readmit_Days and vw_MedAssets_DRG_Tables?
ASKER CERTIFIED SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
GPSPOW

ASKER
Thanks

Glen