Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.04 KB | None | 0 0
  1. DECLARE @NonCoveredID UNIQUEIDENTIFIER = NULL;
  2. DECLARE @End DATE = CONVERT(DATE, GETDATE());
  3. SELECT
  4.     f.FacilityName
  5.     , p.PatientName
  6.     , nc.RequestDate
  7.     , nc.Discipline
  8.     , nc.Frequency
  9.     , nc.Weeks
  10.     , nc.Status
  11.     , nc.DescisionDate
  12.     , nc.ApprovedBy
  13.     , nc.ProjectedEquipmentNeeds
  14.     , nc.SponsorInformedOfTreatment
  15.     , nc.ABNToSponsorDate
  16.     , mfsi.Rate
  17.     --including this because I'm sort of interested in whether I'm using the correct rate; pretty sure this is a "bad" rate column.
  18.     --, NonCoveredItemRate = nci.Rate
  19.     , nci.Units
  20.     , nci.CPT
  21.     -- # of units * med fee sched rate * [if eval, 1, else frequency * weeks]
  22.     -- * contract bps percentage for "Non Cov" 
  23.     -- we presume these patients to be, because they have non covered items.
  24.     , FacilityCost =
  25.         CONVERT(DECIMAL(18,2)
  26.             , nci.Units * mfsi.Rate * IIF(tcc.Eval = 1, 1, nc.Frequency * nc.Weeks) * bps.Percentage
  27.         )
  28. FROM Facility.Facility AS f
  29. JOIN dbo.DARPatient AS p ON p.FacilityID = f.FacilityID
  30.  
  31. JOIN dbo.NonCovered AS nc ON nc.PatientID = p.DARPtID
  32. JOIN dbo.NonCoveredItem AS nci ON nci.NonCoveredID = nc.NonCoveredID
  33. --used to determined whether the cpt (non covered item) is an eval
  34. LEFT JOIN Billing.TemplateCPTCode AS tcc ON tcc.Code = nci.CPT
  35. --gets the billing payer source percentage for this contract as though the payer source is 'non cov' (non covered).
  36. OUTER APPLY (SELECT TOP 1 bps.Percentage
  37.     FROM Billing.Contract AS bc
  38.     JOIN Billing.BillingPayerSource AS bps ON bps.ContractID = bc.ID
  39.         AND bps.Name = 'Non Cov'
  40.         AND bps.EffectiveDate <= @End
  41.     WHERE bc.Name = f.CurrentContract
  42.     ORDER BY bps.EffectiveDate desc) AS bps
  43. --gets the rate for this facility's med fee sched for this particular cpt (non covered item)
  44. OUTER APPLY (SELECT TOP 1 mfsi.Rate FROM MedFeeSched AS mfs
  45.     JOIN dbo.MedFeeSchedItems AS mfsi ON mfsi.MedFeeScheduleID = mfs.MedFeeScheduleID
  46.     WHERE mfs.Name = f.MedFeeSchedName
  47.         AND mfsi.CPT = nci.CPT
  48.         AND mfs.EffectiveDate <= nc.RequestDate
  49.     ORDER BY mfs.EffectiveDate DESC) AS mfsi
  50. WHERE (nc.NonCoveredID = @NonCoveredID OR @NonCoveredID IS NULL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement