SQL如果存在于临时表

时间:2017-11-06 19:39:21

标签: sql sql-server

我正在查看大量的结算和应收帐款数据。我们想知道哪些患者仍有余额。这些数据包括账单和现金收款,因此当患者“还清”账户时,很多金额总和为零。计费数据为3.8M行。我创建的临时表告诉我哪些患者仍然有付款人的余额。我想在3.8M行数据上创建一个列,告诉我患者和付款人是否在临时表上存在“是”或“否” - 这将告诉我数据行是否仍然与现金收集目的相关,或者因为它已被收集而被忽略。

运行以下内容时遇到以下错误:

  

Msg 156,Level 15,State 1,Line 79
  关键字“IF”附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 80
  ','。`

附近的语法不正确

错误从“IF存在”行开始,从底部开始大约12行。如果这很难读,我会提前道歉;这是我第一次提出问题,而且我在编写查询方面不是最有经验的,但过去两年我来到这个网站很多,并且总是找到我需要的东西。我可能会在我的头上,但感谢您的帮助。

请告诉我您可能遇到的问题。

--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'

1 个答案:

答案 0 :(得分:2)

您在查询中无法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