简体   繁体   English

SQL如果在临时表中存在

[英]SQL If Exists in temporary table

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. 帐单数据为380万行。 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. 我想在380万行数据上创建一列,告诉我临时表上是否存在患者和付款人,“是”或“否”-这将告诉我数据行是否仍然与现金收集目的相关,或者由于已经收集而可以忽略不计。

I run into the following errors when I run the below: 运行以下命令时遇到以下错误:

Msg 156, Level 15, State 1, Line 79 信息156,第15层,州1,第79行
Incorrect syntax near the keyword 'IF'. 关键字“ IF”附近的语法不正确。

Msg 102, Level 15, State 1, Line 80 Msg 102,第15级,状态1,第80行
Incorrect syntax near ','.` ','附近的语法不正确。

The error starts on the "IF Exists" row about 12 rows from the bottom. 错误从底部大约12行的“ IF Exists”行开始。 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: 查询中不能包含IF改用CASE

, 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM