I'm looking at a lot of billing and accounts receivable data. We want to know which patients still have balances. The data consists of billing and cash collections, so a lot of the amounts sum to zero when a patient has "paid off" their account. The billing data is 3.8M rows. The temp table I've created tells me which patients still have a balance by payer. I'd like to create a column on the 3.8M rows of data that tells me if the patient and payer exists on the temp table with a "yes" or a "no" - this will tell me whether or not the row of data is still relevant for cash collection purposes or if it can be ignored because it's already collected.
I run into the following errors when I run the below:
Msg 156, Level 15, State 1, Line 79
Incorrect syntax near the keyword 'IF'.Msg 102, Level 15, State 1, Line 80
Incorrect syntax near ','.`
The error starts on the "IF Exists" row about 12 rows from the bottom. I apologize in advance if this is hard to read; this is my first time asking a question and I'm not the most experienced in writing queries but I've come to this website a lot of the past two years and always found what I needed until now. I may be in over my head, but thanks for your help in advance.
Please let me know what questions you may have.
--TempTable
SELECT
VIEW_AR_AGING.FACILITYID,
client.LastName AS 'Last Name',
client.firstname AS 'First Name',
client.PatientIDNumber,
VIEW_AR_AGING.patientid,
VIEW_AR_AGING.payerid,
payer.payercode, payer.payercode2,
payer.PayerName as 'Payer',
FORMAT (SUM(VIEW_AR_AGING.amount), 'N', 'en-us') AS 'Amount',
CONCAT(client.LastName, '-', VIEW_AR_AGING.patientid, '-', VIEW_AR_AGING.payerid) AS 'Unique_Patient_Payer'
INTO
#TempSummary
FROM
view_ods_ar_transaction_detail VIEW_AR_AGING
LEFT JOIN
dbo.view_ods_facility_patient client ON VIEW_AR_AGING.patientid = client.PatientID AND (client.PatientDeleted = 'N')
LEFT JOIN
view_ods_payer payer ON VIEW_AR_AGING.payerid = payer.payerid
LEFT JOIN
ar_lib_accounts a ON VIEW_AR_AGING.DollarsAccountID = a.account_id
WHERE
VIEW_AR_AGING.transactionid IS NOT NULL
AND VIEW_AR_AGING.transactiondate <= eomonth(GETDATE())
AND VIEW_AR_AGING.amount IS NOT NULL AND VIEW_AR_AGING.amount <> 0
AND VIEW_AR_AGING.patientid > 0
AND (VIEW_AR_AGING.FACILITYID <>9 or VIEW_AR_AGING.FACILITYID <>10 OR VIEW_AR_AGING.FACILITYID = -1)
GROUP BY
VIEW_AR_AGING.patientid, VIEW_AR_AGING.FACILITYID,
client.LastName, client.firstname, client.PatientIDNumber,
VIEW_AR_AGING.patientid, VIEW_AR_AGING.payerid,
payer.payercode, payer.payercode2, payer.PayerName
HAVING
SUM(VIEW_AR_AGING.amount) <> 0
--End TempTable
SELECT
VIEW_AR_AGING.FACILITYID,
client.LastName AS 'Last Name',
client.firstname AS 'First Name',
CONVERT(VARCHAR(10), client.OriginalAdmissionDate, 110) AS 'Original Admission Date',
CONVERT(VARCHAR(10), client.RecentAdmissionDate, 110) AS 'Recent Admission Date',
CONVERT(VARCHAR(10), client.DischargeDate, 110) AS 'Discharge Date',
CONVERT(VARCHAR(10), client.DeceasedDate, 110) AS 'Deceased Date',
client.PatientIDNumber,
VIEW_AR_AGING.patientid, VIEW_AR_AGING.payerid,
payer.payercode, payer.payercode2, payer.PayerName as 'Payer',
VIEW_AR_AGING.transactiontype, VIEW_AR_AGING.DollarsAccountID,
a.description Account_Desc, a.consolidation_account Account_GL,
CONVERT(VARCHAR(10), VIEW_AR_AGING.transactiondate, 110) AS 'Transaction Date',
CONVERT(VARCHAR(10), VIEW_AR_AGING.effectivedate, 110) AS 'Effective Date',
VIEW_AR_AGING.amount AS 'Amount',
CONCAT(client.LastName, '-', VIEW_AR_AGING.patientid, '-', VIEW_AR_AGING.payerid) AS 'Unique_Patient_Payer',
IF EXISTS (SELECT * FROM #TempSummary b
WHERE b.CONCAT(client.LastName, '-', VIEW_AR_AGING.patientid, '-', VIEW_AR_AGING.payerid) = CONCAT(client.LastName, '-', VIEW_AR_AGING.patientid, '-', VIEW_AR_AGING.payerid)), 'yes', 'no') AS Has_Balance
FROM
view_ods_ar_transaction_detail VIEW_AR_AGING
LEFT JOIN
dbo.view_ods_facility_patient client ON VIEW_AR_AGING.patientid = client.PatientID AND (client.PatientDeleted = 'N')
LEFT JOIN
view_ods_payer payer ON VIEW_AR_AGING.payerid = payer.payerid
LEFT JOIN
ar_lib_accounts a ON VIEW_AR_AGING.DollarsAccountID = a.account_id
WHERE
VIEW_AR_AGING.transactionid IS NOT NULL
AND VIEW_AR_AGING.transactiondate <= eomonth(GETDATE())
AND VIEW_AR_AGING.amount IS NOT NULL AND VIEW_AR_AGING.amount <> 0
AND VIEW_AR_AGING.patientid > 0
AND (VIEW_AR_AGING.FACILITYID <>9 or VIEW_AR_AGING.FACILITYID <>10 OR VIEW_AR_AGING.FACILITYID = -1)
AND VIEW_AR_AGING.transactiondate > '2017-07-01'
You can't have IF
within a query - use CASE
instead:
, CASE WHEN EXISTS (SELECT *
FROM #TempSummary b
WHERE b.concat(client.LastName,
'-',VIEW_AR_AGING.patientid,'-',VIEW_AR_AGING.payerid)
= concat(client.LastName,
'-',VIEW_AR_AGING.patientid,'-',VIEW_AR_AGING.payerid))
THEN 'yes'
ELSE 'no' END AS Has_Balance
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.