我的SQL结尾的语法不正确?

时间:2014-06-24 16:50:26

标签: sql sql-server database left-join

我正在尝试运行此SQL代码来测试它需要多长时间。我知道它没有正确格式化,但我没有写它,我只是调试它来测试我做的一些更改/添加,所以请不要评论写得不好这是因为这并不能帮助我解决问题。我想知道为什么我在代码结尾处收到以下错误:

  

Msg 102,Level 15,State 1,Line 353
  ')'附近的语法不正确。

这是sql代码:

SELECT   
         t.[SiteID]
        ,s.[Name] AS SiteName
        ,[Date: Create Date]                     = convert(datetime, convert(varchar, t.CreateDate,101)) 
        ,[Date: Date of Service]                 = DateOfService
        ,[Date: Original Posting Date]               = ParentPostingDate
        ,[Date: Original Create Date]                = convert(datetime, convert(varchar, ParentCreateDate,101))
        ,[Date: Posting Date]                    = PostingDate
        ,[Orginal Transaction: Transaction Amount]       = Amount
        ,[Original Transaction: Payment/Adjustment Source]   = PaymentSource
        ,[Patient: Date Of Birth]                = DOB
        ,[Patient: Sex]                      = Sex
        ,[Patient: Zip Code]                     = ZipCode
        ,[Patient: Name]                     = PatientName
        ,[Patient: Patient ID]                   = t.PatientID
        ,[Patient: Preferred Provider]      = PreferredProvider
        ,[Patient: Preferred Provider ID]       = PreferredProviderID
        ,[Patient: Address Line 1]          = AddressLine1
        ,[Patient: Address Line 2]          = AddressLine2
        ,[Patient: Age]                     = Age
        ,[Patient: City]                    = City
        ,[Patient: County]                  = County
        ,[Patient: Credentials]             = Credentials
        ,[Patient: Deceased Flag]           = DeceasedFlag
        ,[Patient: EMail]                   = t.Email
        ,[Patient: Employer]                = Employer
        ,[Patient: First Name]              = FirstName
        ,[Patient: Last Name]               = LastName
        ,[Patient: Middle Initial]          = MiddleInitial
        ,[Patient: Middle Name]             = MiddleName
        ,[Patient: Phone Home]              = PhoneHome
        ,[Patient: Phone Primary]           = PhonePrimary
        ,[Patient: Phone Work]              = PhoneWork
        ,[Patient: Preferred Name]          = PreferredName
        ,[Patient: Other ID]                = OtherID
        ,[Patient: Prefix]                  = Prefix
        ,[Patient: Race]                    = Race
        ,[Patient: Ethnicity]               = Ethnicity 
        ,[Patient: Preferred Communications]    = PreferredCommunications   
        ,[Patient: SSN]                     = SSN
        ,[Patient: State]                   = State
        ,[Patient: Suffix]                  = Suffix    
        ,[Transaction: Create Dates Equal]           = CASE WHEN convert(datetime, convert(varchar, t.CreateDate,101)) = convert(datetime, convert(varchar, ParentCreateDate,101)) THEN 'Yes' Else 'No' END
        ,[Transaction: Posting Dates Equal]          = CASE WHEN PostingDate = ParentPostingDate THEN 'Yes' Else 'No' END
        ,[Transaction: Create and Posting Dates Equal]       = CASE WHEN PostingDate = convert(datetime, convert(varchar, t.CreateDate,101)) THEN 'Yes' Else 'No' END
        ,[(1-CostOfService)*(NetPayments)] = (1 - CostofService) * (NetAppliedPayments)
        ,[Transaction: Adjustment Sub-Type]          = AdjustmentSubType
        ,[Transaction: Adjustment Type]              = AdjustmentType
        ,[Transaction: Amount - Charge Voids]            = ChargeVoids
        ,[Transaction: Amount - Gross Adjustments]       = GrossAppliedAdjustments
        ,[Transaction: Amount - Gross Charges]           = GrossCharges
        ,[Transaction: Amount - Gross Payments]          = GrossAppliedPayments
        ,[Transaction: Amount - Transaction Amount]      = TransactionAmount
        ,[Transaction: Amount - Adjustment Offsets]      = AppliedAdjustmentOffsets
        ,[Transaction: Amount - Adjustment Voids]        = AppliedAdjustmentVoids
        ,[Transaction: Amount - Gross Insurance Payments]    = GrossAppliedPaymentsIns
        ,[Transaction: Amount - Gross Patient/Other Payments]    = GrossAppliedPaymentsPatOth
        ,[Transaction: Amount - Net Adjustments]         = NetAppliedAdjustments
        ,[Transaction: Amount - Net Charges]             = NetCharges
        ,[Transaction: Amount - Net Payments]            = NetAppliedPayments
        ,[Transaction: Amount - Payment Offsets]         = AppliedPaymentOffsets
        ,[Transaction: Amount - Payment Voids]           = AppliedPaymentVoids
        ,[Transaction: Batch ID]                 = convert(varchar,BatchID)--CASE BatchID WHEN 0 THEN 'N/A' WHEN NULL THEN 'N/A' ELSE convert(varchar,BatchID) END  
        ,[Transaction: Batch Type ID]            = CAST(BatchStateID AS INT)
        ,[Transaction: Batch Type]               = BatchState--CASE BatchState WHEN 1 then 'Open' WHEN 0 then 'Open' WHEN 2 then 'Reviewed' else 'N/A' END
        ,[Transaction: Batch Reviewed Date]          = convert(datetime, convert(varchar, BatchClosedDate,101)) 
        ,[Transaction: Billable Provider Name]           = isnull(BillableProvider, 'No Billable Provider')
        ,[Transaction: Billable Provider ID]             = isnull(BillableProviderID,-1) 
        ,[Transaction: Charge Override Indicator]        = ChargeOverrideIndicator
        ,[Transaction: Charge Ticket Number]             = ChargeTicketNumber
        ,[Transaction: Description]              = Description
        ,[Transaction: Diagnosis Codes]              = Diagnosis
        ,[Transaction: Diagnosis 1]              = Diagnosis1
        ,[Transaction: Diagnosis 2]              = Diagnosis2
        ,[Transaction: Diagnosis 3]              = Diagnosis3
        ,[Transaction: Diagnosis 4]              = Diagnosis4
        ,[Transaction: ERA Indicator]                = ERAIndicator
        ,[Transaction: Payment Method]               = PaymentMethod
        ,[Transaction: Payment Supplier]             = PaymentSupplier
        ,[Transaction: PaymentID]    = PaymentID
        ,[Transaction: Payment Sub Type] = PaymentSubType
        ,[Transaction: Check Number]     = isnull(CheckNumber, 'N/A')
        ,[Transaction: Payment/Adjustment Additional Info]   = isnull(PaymentAdditionalInformation, 'N/A')
        ,[Transaction: Payment/Adjustment Source]        = PaymentSource
        ,[Transaction: Practice Location]            = PracticeLocation
        ,[Transaction: Practice Location ID]             = PracticeLocationID
        ,[Transaction: Procedure Code]               = ProcedureCode
        ,[Transaction: Procedure Code Alternate]         = AlternateCode
        ,[Transaction: Procedure Cost of Service]    = CostofService
        ,[Transaction: Procedure Description]        = ProcedureDescription
        ,[Transaction: Procedure Modifiers]          = Modifiers
        ,[Transaction: Refund Check Number]          = RefundCheckNumber
        ,[Transaction: Refund Payee]                 = isnull(RefundPayee,'')
        ,[Transaction: Refund Address Line 1] =         RefundAdd1 
        ,[Transaction: Refund Address Line 2] =         RefundAdd2 
        ,[Transaction: Refund City] =                   RefundCity 
        ,[Transaction: Refund State] =                  RefundState
        ,[Transaction: Refund Zip Code] =               RefundZip  
        ,[Transaction: Rendering Provider Name]          = isnull(RenderingProvider, 'No Rendering Provider')
        ,[Transaction: Rendering Provider ID]            = isnull(RenderingProviderID,-1) 
        ,[Transaction: Revenue Category]             = RevenueCategory
        ,[Transaction: Referring Provider Name]          = isnull(ReferringProvider, 'No Referring Provider')
        ,[Transaction: Referring Provider ID]            = isnull(ReferringProviderID,-1)
        ,[Transaction: Revenue Category ID]          = RevenueCategoryID
        ,[Transaction: Service Location]             = ServiceLocation
        ,[Transaction: Service Location ID]          = ServiceLocationID
        ,[Transaction: Type]                     = ltrim(TranType)
        ,[Transaction: Expanded Type]                = case when AdjustmentTypeID is null then ltrim(TranType)  when AdjustmentTypeID = 0 then (ltrim(TranType) + '-PMT') else ltrim(TranType)+'-ADJ'end
        ,[Transaction: UserID]                   = UserID
        ,[Transaction: User]                     = UserName
        ,[Transaction: User Initials]                = UserInitials
        ,[Transaction: Visit ID]                 = isnull(VisitID, 'N/A')
        ,[Transaction: Visit Type]               = VisitType
        ,[Transaction: Visit Type ID]                = VisitTypeID
        ,[Transaction: Void Indicator]               = VoidIndicator
        ,[Transaction: Parent Type]              = replace(replace(replace(TranType, 'V-', ''), '-FROM', ''),'-TO', '')
        ,[Transaction: Applied To]               = CASE WHEN CreditCategory is not null THEN CreditCategory  WHEN ProcedureCode is not null THEN 'Procedure Code' ELSE Null END
        ,[Transaction: Net Charge Units]         = NetChargeUnits
        ,[Transaction: Create Date Time]             = CreateDateTime
        ,[Transaction: Charge - RVUWork]    = convert(decimal(10,5),RVUWork)
        ,[Transaction: Charge - RVUMP]      = convert(decimal(10,5),RVUMP)
        ,[Transaction: Charge - RVUNFPE]    = convert(decimal(10,5),RVUNFPE)
        ,[Transaction: Charge - RVUNFTotal] = convert(decimal(10,5),RVUNFTotal)
        ,[Transaction: Charge - RVUFPE]     = convert(decimal(10,5),RVUFPE)
        ,[Transaction: Charge - RVUFTotal]  = convert(decimal(10,5),RVUFTotal)
        ,[Transaction: Write Off at Charge Entry Contractuals] = ChargeEntryWriteOffContractual
        ,TransactionAmountPatient
        ,TransactionAmountOther
        ,TransactionAmountInsurance
        ,TransactionAmount
        ,DepositFlag
        ,AllocationID
        ,QueryIndex
        ,CreditRCMProviderID
        ,CreditRCMCareProviderLocationID 
        ,CreditCareProvider
        ,CreditCareProviderLocation
    FROM (
    SELECT 
         CreateDate                     = pa.CreatedOn
        ,CreateDateTime                 = pa.CreatedOn
        ,PostingDate                    = Pa.PostingDate
        ,UserID                         = u.UserID
        ,UserName                       = u.UserName
        ,UserInitials                   = UPPER(CASE WHEN ISNULL(U.FirstName,'') = '' 
                                            THEN ''
                                        ELSE (LEFT(U.FirstName,1)) 
                                        END
                                        + CASE WHEN ISNULL(U.LastName,'') = '' 
                                            THEN ''
                                        ELSE LEFT(U.LastName,1)
                                        END)
        ,ProcedureCode                  = c.ProcedureCode
        ,CreditCategory                 = CT.Description
        ,ChargeTicketNumber             = v.VisitID --In PS this is ChargeTicketID which seems to always be the VisitID
        ,DateOfService                  = c.ServiceDate
        ,RenderingProvider              = ren.name
        ,RenderingProviderID            = c.Rendering_RCMCareProviderID
        ,BillableProvider               = bill.name
        ,BillableProviderID             = c.Billable_RCMCareProviderID
        ,PreferredProvider              = pref.name
        ,PreferredProviderID            = patient.CareProviderID 
        ,ReferringProviderID            = c.Referring_RCMCareProviderID
        ,ReferringProvider              = ref.name
        ,ServiceLocation                = c.ServiceLocation
        ,ServiceLocationID              = c.Service_RCMCareProviderLocationID
        ,PracticeLocation               = c.PracticeLocation
        ,PracticeLocationID             = c.Practice_RCMCareProviderLocationID
        ,VisitType                      = ISNULL(VType.LocalName, 'No Visit Type')
        ,VisitTypeID                    = VType.RCMVisitTypesID
        ,VisitID                        =  CASE WHEN V.VisitID IS NULL THEN 'N/A' WHEN V.VisitId = 0 THEN 'N/A' ELSE convert(varchar,V.VisitID) END
        ,RevenueCategory                = CASE WHEN prc.RevenueCategoryName IS NULL THEN 'No Revenue Category' ELSE prc.RevenueCategoryName END 
        ,RevenueCategoryID              = pmi.RevenueCategoryID
        ,BatchID                        = P.PaymentBatchID /*CASE pa.BatchID WHEN 0 THEN 'N/A' WHEN NULL THEN 'N/A' ELSE convert(varchar,pa.BatchID) END  */
        ,PatientName                    = f.LNameFName

        ,AddressLine1                   =   f.Address1
        ,AddressLine2                   =   f.Address2
        ,Age                            =   f.Age
        ,City                           =   f.City
        ,County                         =   f.County
        ,Credentials                    =   f.CredentialsString
        ,DeceasedFlag                   =   CASE
                                        WHEN patient.PatDeathFlag   =1 
                                            THEN 'Yes'
                                        ELSE 'No'
                                        END
        ,Email                          =   f.Email
        ,Employer                       =   f.PrimaryEmployer
        ,FirstName                      =   f.FirstName
        ,LastName                       =   f.LastName
        ,MiddleInitial                  =   CASE 
                                            WHEN f.MiddleName1  = '' 
                                                THEN ''
                                            ELSE Left(f.MiddleName1,1) + '. '
                                            END
        ,MiddleName                     =   f.MiddleName1
        ,PhoneHome                      =   f.HomePhone
        ,PhonePrimary                   =   f.PrimaryPhone
        ,PhoneWork                      =   f.PrimaryWorkPhone
        ,PreferredName                  =   f.NickName
        ,OtherID                        =   f.OrgPatientID
        ,Prefix                         =   f.Prefix
        ,Race                           =   f.Race
        ,Ethnicity                      =   f.Ethnicity
        ,PreferredCommunications        =   f.PreferredCommunications
        ,SSN                            =   f.SSN
        ,State                          =   f.State
        ,Suffix                         =   f.Suffix    

        ,DOB                            = convert(datetime,FORMAT(f.DateOfBirth, 'MM/dd/yyyy'))
        ,Sex                            = f.Sex
        ,ZipCode                        = f.PostalCode
        ,PatientID                      = Patient.PatientID
        ,TransactionAmount              = pa.UserAmount
        ,TransactionAmountPatient       = CASE WHEN P.PaymentSourceID = 1 THEN pa.UserAmount ELSE 0 END
        ,TransactionAmountInsurance     = CASE WHEN P.PaymentSourceID = 2 THEN pa.UserAmount ELSE 0 END
        ,TransactionAmountOther         = CASE WHEN P.PaymentSourceID = 3 THEN pa.UserAmount ELSE 0 END
        ,VoidIndicator                  = CASE 
                                        WHEN pa.AllocationTypeID BETWEEN 2000 AND 3999 AND pa.PaymentAllocationID = pa.Offset_PaymentAllocationID 
                                            THEN 'YES' 
                                        WHEN TT.Description = 'V-PMT' OR TT.Description = 'V-C-ADJ' OR TT.Description = 'V-D-ADJ' OR TT.Description ='V-TKB' 
                                            THEN 'YES'
                                        WHEN pa.AllocationTypeID NOT BETWEEN 2000 AND 3999 AND p.Voided <> 0 AND TT.Description NOT LIKE 'V%' 
                                            THEN 'YES' 
                                        ELSE 'NO' 
                                        END
        ,ChargeOverrideIndicator        = 'NO'
        ,NetChargeUnits = 0
        ,ERAIndicator                   = CASE WHEN P.ERAIndicator <> 1 THEN 'NO' ELSE 'YES' END
        ,AlternateCode                  = pmi.AlternateProcedureCode
        ,CostOfService                  = pmi.CostOfService
        ,ProcedureDescription           = pmi.StandardDescription
        ,Modifiers                      = c.ModList
        ,Diagnosis                      = isnull(sd.DiagnosisCode1, '') + ' ' +  isnull(sd.DiagnosisCode2, '') + ' ' +  isnull(sd.DiagnosisCode3, '') + ' ' +  isnull(sd.DiagnosisCode4, '')                      
        ,Diagnosis1                     = sd.DiagnosisCode1
        ,Diagnosis2                     = sd.DiagnosisCode2
        ,Diagnosis3                     = sd.DiagnosisCode3
        ,Diagnosis4                     = sd.DiagnosisCode4
        ,PaymentSource                  = PS.Description

        ,PaymentMethod                  = PM.Description
        ,PaymentSupplier                = S.Description
        ,PaymentAmount                  = pa.Amount
        ,PaymentID                      = p.PaymentID
        ,CheckNumber                    = p.CheckNumber
        ,PaymentAdditionalInformation   = p.CheckNumber
        ,AdjustmentType                 = ADT.Description
        ,AdjustmentSubType              = AdjustmentSubTypeDesc
        ,RefundPayee                    = ISNULL(i.Payee,i2.Payee)
        ,RefundCheckNumber              = ISNULL(i.CheckNumber,i2.CheckNumber) 
        ,RefundAdd1                     = COALESCE(iAdd.AddressLine1,i2Add.AddressLine1,'')
        ,RefundAdd2                     = COALESCE(iAdd.AddressLine2,i2Add.AddressLine2,'')
        ,RefundCity                     = COALESCE(iAdd.City,i2Add.City,'')
        ,RefundState                    = COALESCE(iAdd.State,i2Add.State,'')
        ,RefundZip                      = COALESCE(iAdd.PostalCode,i2Add.PostalCode,'')
        ,Type                           = PA.AllocationTypeID
        ,AdjustmentTypeID               = pa.AdjustmentTypeID                
        ,Amount                         = p.amount * -1
        ,TranType                       = TT.Description
        ,GrossAppliedPayments           = pa.GrossAppliedPmt            
        ,NetAppliedPayments             = pa.NetAppliedPmt              
        ,AppliedPaymentVoids            = pa.AppliedPmtVoid             
        ,AppliedPaymentOffsets          = pa.AppliedPmtOffset           
        ,GrossAppliedAdjustments        = pa.GrossAppliedAdj         
        ,AppliedAdjustmentVoids         = pa.AppliedAdjVoid          
        ,AppliedAdjustmentOffsets       = pa.AppliedAdjOffset        
        ,NetAppliedAdjustments          = pa.NetAppliedAdj
        ,PaymentDate                    = p.PostingDate 
        ,BatchStateID                   = pbs.PaymentBatchStatusID 
        ,BatchState                     = pbs.Description /*CASE bu.State WHEN 1 then 'Open - Active' WHEN 0 then 'Open - Inactive' WHEN 2 then 'Reviewed' else 'N/A' END*/
        ,BatchClosedDate                = bar.DepositDate
        ,GrossAppliedPaymentsPatOth     = pa.GrossAppliedPmtPatOther
        ,GrossAppliedPaymentsIns        = pa.GrossAppliedPmtIns
        ,NetCharges                     = 0
        ,ChargeVoids                    = 0
        ,GrossCharges                   = 0
        ,ParentPostingDate              = p.PostingDate 
        ,ParentCreateDate               = p.CreatedOn
        ,DepositFlag                    = CASE WHEN p.PaymentMethodID in (1,2,5) THEN 1 ELSE 0 END                                
        ,Description                    = pa.Description 
        ,AllocationID                   = pa.PaymentAllocationID
        ,QueryIndex                     = 1
        ,RVUWork                        = null
        ,RVUMP                          = null
        ,RVUNFPE                        = null
        ,RVUNFTotal                     = null
        ,RVUFPE                         = null
        ,RVUFTotal                      = null
        ,ChargeEntryWriteOffContractual = CASE 
                                          WHEN c.PostingDate = pa.postingDate AND ADT.Description = 'Contractual' 
                                            THEN pa.NetAppliedAdj 
                                          ELSE 0 
                                          END
        ,PA.SiteID
        ,pst.Description AS 'PaymentSubType'
        ,CreditRCMProviderID            = ISNULL(psi.RCMCareProviderID, 0)
        ,CreditRCMCareProviderLocationID = ISNULL(psi.RCMCareProviderLocationID, 0)
        ,CreditCareProvider             = ISNULL(C_Name.Name, 'No Credited Provider')
        ,CreditCareProviderLocation     = ISNULL(cloc.LocationName, 'No Credit Location')
        ,CASE
        WHEN ISNULL(icdVer.CodingSystemID, 1020) = 1020 AND syt.DiagnosisCodingEffectiveDate IS NULL
            THEN sd.DiagnosisCode1
        WHEN icdVer.CodingSystemID = 1016 OR (syt.DiagnosisCodingEffectiveDate IS NOT NULL AND syt.DiagnosisCodingEffectiveDate < GETDATE())
            THEN sd.ICD10DiagnosisCode1
        ELSE ''
        END AS DiagnosticCode1
    FROM 
        Account.vAllocationMoneyDescribe PA (NOLOCK)
        JOIN Account.fctCharge c (NOLOCK) ON pa.ChargeID = c.ChargeID
        LEFT JOIN Account.AllocationType VT (NOLOCK) ON vt.AllocationTypeID = PA.AllocationTypeID
        LEFT JOIN Account.BillingTransactionType TT ON TT.BillingTransactionTypeID = pa.BillingTransactionTypeID
        LEFT JOIN Account.Payment p ON p.PaymentID = p.PaymentID
        LEFT JOIN Account.PaymentBatch batch ON batch.PaymentBatchID = p.PaymentBatchID
        LEFT JOIN PrimeSuite.Patient patient (NOLOCK) ON patient.RCMPatientID = pa.RCMPatientID
        LEFT JOIN PrimeSuite.fctPerson f (NOLOCK) ON f.SiteID = Patient.SiteID AND f.PatientID = patient.PatientID
        LEFT JOIN PrimeSuite.InsuranceCoverage cover ON cover.RCMInsuranceCoverageID = p.RCMInsuranceCoverageID
        LEFT JOIN PrimeSuite.InsurancePlan insPlan ON insPlan.PlanID = cover.PlanID
                  ANd insPlan.SiteID = cover.SiteID
        LEFT JOIN Account.ChargeDetail sd (NOLOCK) ON pa.ChargeID = sd.ChargeID
        LEFT JOIN Encounter.Visit V (NOLOCK) ON sd.VisitID = v.VisitID
        LEFT JOIN Division.[Site] syt ON syt.SiteID = v.SiteID
        LEFT JOIN PrimeSuite.VisitTypes VType ON VType.RCMVisitTypesID = V.RCMVisitTypeID
        LEFT JOIN PrimeSuite.ProcedureMasterInfo pmi (NOLOCK) ON PA.SiteID = PMI.SiteID AND sd.ProcedureMasterID = pmi.ProcedureMasterID
        LEFT JOIN PrimeSuite.ProcedureRevenueCategory prc (NOLOCK) ON prc.SiteID = PMI.SiteiD AND pmi.RevenueCategoryID = prc.RevenueCategoryID
        LEFT JOIN Account.PaymentAllocationRefundInformation i (NOLOCK) ON pa.PaymentAllocationID = i.PaymentAllocationID
        LEFT JOIN Account.PaymentAllocationRefundInformation i2 (NOLOCK) ON pa.Originator_PaymentAllocationID = i2.PaymentAllocationID 
        LEFT JOIN PrimeSuite.Address iAdd ON iAdd.RCMAddressID = i.Payee_RCMAddressID
        LEFT JOIN PrimeSuite.Address i2Add ON i2Add.RCMAddressID = i2.Payee_RCMAddressID
        INNER JOIN [Security].[User] U ON U.UserID = PA.CreatedBy_UserID
        LEFT JOIN Account.PaymentSource ps (NOLOCK) ON p.PaymentSourceID = ps.PaymentSourceID
        LEFT JOIN Account.PaymentMethod pm (NOLOCK) ON p.PaymentMethodID  = pm.PaymentMethodID
        LEFT JOIN Account.CreditType ct (NOLOCK) ON vt.Credit = ct.CreditTypeID
        LEFT JOIN Account.PaymentSupplier s (NOLOCK) ON p.PaymentSupplierID = s.PaymentSupplierID
        LEFT JOIN Account.AdjustmentType adt (NOLOCK) ON p.AdjustmentTypeID = adt.AdjustmentTypeID
        LEFT JOIN PrimeSuite.AdjustmentSubType st (NOLOCK) ON p.RCMAdjustmentSubTypeID = st.RCMAdjustmentSubTypeID
        LEFT JOIN Account.PaymentBatch bar (NOLOCK) ON P.PaymentBatchID = bar.PaymentBatchID
        LEFT JOIN Account.PaymentBatchStatus PBS ON PBS.PaymentBatchStatusID = BAR.PaymentBatchStatusID
        LEFT JOIN [Utility].[udfNameFormat]('%Last,% %First% %MiddleInitial% %Suffix% %Credential%') bill ON bill.SiteID = C.SiteID AND bill.CPID = C.BillableCareProviderID
        LEFT JOIN [Utility].[udfNameFormat]('%Last,% %First% %MiddleInitial% %Suffix% %Credential%') ren ON ren.SiteID = C.SiteID AND ren.CPID = C.RenderingProviderID
        LEFT JOIN [Utility].[udfNameFormat]('%Last,% %First% %MiddleInitial% %Suffix% %Credential%') ref ON ref.SiteID = C.SiteID AND ref.CPID = C.ReferringProviderID
        LEFT JOIN [Utility].[udfNameFormat]('%Last,% %First% %MiddleInitial% %Suffix% %Credential%') pref ON pref.SiteID = C.SiteID AND pref.CPID = patient.CareProviderID
        LEFT JOIN Account.PaymentOnSiteInformation psi ON p.PaymentID = psi.PaymentID
        LEFT JOIN PrimeSuite.CareProviderLocation cloc (NOLOCK) ON cloc.RCMCareProviderLocationID = psi.RCMCareProviderLocationID
        LEFT JOIN PrimeSuite.CareProvider CREDP ON CREDP.RCMCareProviderID = psi.RCMCareProviderID
        LEFT JOIN [Utility].[udfNameFormat]('%Last,% %First% %MiddleInitial% %Suffix% %Credential%') C_Name ON C_Name.SiteID = CREDP.SIteID AND C_Name.CPID = CREDP.CareProviderID
        LEFT JOIN Account.PaymentSubType pst ON psi.PaymentSubTypeID = pst.PaymentSubTypeID
        LEFT JOIN ICD.ICDVersion icdVer ON icdVer.CodingSystemID = insPlan.DiagnosisCodingSystemID

    WHERE 
       vt.Credit = 0 
       AND PA.[SiteID] = 9
       AND c.VoidType <> 'V'
       AND NOT PA.BillingTransactionTypeID IN (11, 12, 23, 24)

1 个答案:

答案 0 :(得分:2)

这是很多代码,所以我可能错过了它,但看起来你似乎没有关闭嵌套查询。简而言之,你有这个:

SELECT *
FROM (
   SELECT *
   FROM Foo
   WHERE Foo.Bar IN (11,12,23,24)

你需要这个:

SELECT *
FROM (
    SELECT *
    FROM Foo
    WHERE Foo.Bar IN (11,12,23,24)
) f