I am trying to modify the following query so that it will not pull results where we have more records for a given account in #tempBarTransactions than we do in the view_finalizedVisitsProcedureDetail. In other words, currently the following query will return results for an account that has several records in the #tempBarTransactions table and only one record in the view_finalizedVisitsProcedureDetail. Since bt.charge matches fv.visitAmount for that one that is in both sources, we get a record returned for that account.
Here is the query:
I think the problem lies here:
Since that will only try to match on records that exist in both sources. I need it to match on every record in both sources.
Thanks,
LJ
Here is the query:
Code:
select * from [achri-sql].clinicaltrials.dbo.Scheduled_Visits
WHERE ([is_reconciled] = 0 OR [is_reconciled] IS NULL)
AND id IN (
SELECT fv.visit_fk
--SELECT fv.AccountNumber, fv.ProcedureNumber, fv.VisitQuantity, ISNULL(fv.VisitAmount,0) AS VisitAmount, bt.[PatientAccount], bt.[ProcedureCode], bt.[Quantity], bt.[Charge]
FROM [achri-sql].clinicaltrials.dbo.view_finalizedVisitsProcedureDetail fv
LEFT JOIN dbo.BAR_procedureDictionary p ON p.mnemonic = fv.ProcedureNumber
LEFT JOIN #tempBarTransactionTotals bt ON fv.AccountNumber = bt.[PatientAccount]
AND fv.ProcedureNumber = bt.[ProcedureCode]
AND (fv.VisitQuantity = bt.[Quantity] OR p.department = '014250')
AND fv.visitAmount = bt.charge
AND cast(convert(char(10),fv.[visit_date],120) as datetime) = cast(convert(char(10),bt.ServiceDate,120) as datetime)
WHERE (bt.charge = fv.visitAmount
OR fv.visitAmount = 0)
GROUP BY fv.visit_fk )
I think the problem lies here:
Code:
AND fv.ProcedureNumber = bt.[ProcedureCode]
Since that will only try to match on records that exist in both sources. I need it to match on every record in both sources.
Thanks,
LJ