0

I have this stored procedure that works as intended except for one flaw. I need to account for NULL values when summing up the billing portion of the query. If no value exists, the database defaults to NULL which causes my final calculation to be NULL. To combat this, I thought of using the COALESCE function to force any columns returns to 0 if they were found to be NULL so my calculations work properly.

Currently I am only testing it on the AdjustFeeAmountTotal column in the BillAdjustmentCTE CTE since just using the COALESCE function on the needed columns isn't working. The current implementation that I have below still has the query returning NULL for the AdjustFeeAmountTotal column.

Anyone have a clue why?

 ALTER PROCEDURE [dbo].[GetRollCallData] 
    @Ids        VARCHAR(255),
    @LexiconId  INT,
    @UUID       UNIQUEIDENTIFIER,
    @ReadOnly   INT
 AS

 DECLARE @TableCode INT
 SET @TableCode = 58;

 IF @Ids <> ''
    BEGIN
        EXEC InsertInSelectionCache @Ids, @UUID, @TableCode, 0
        IF @ReadOnly = 1
            With DOACTE AS(
                SELECT ROW_NUMBER() OVER(PARTITION BY [File].Id ORDER BY CustomRecordsetId DESC) AS RowNumber, [File].*, FileType2Lexicon.Label as FileTypeLabel, [People].DefaultPhone, [People].InvertedName as ClientInvertedName, CustomField.Name as FieldLabel, CustomFieldValue.Value as FieldValue
                    FROM FileType2Lexicon, SelectionCache, [People], [File]
                    INNER JOIN [CustomRecordSet]
                    ON [CustomRecordset].RecordId = [File].Id
                    INNER JOIN CustomFieldValue
                    ON  [CustomRecordset].Id = CustomFieldValue.CustomRecordsetId
                    INNER JOIN [CustomField2Lexicon]
                    ON CustomField2Lexicon.CustomFieldId = CustomFieldValue.CustomFieldId
                    INNER JOIN [CustomField]
                    ON CustomField.Id = CustomField2Lexicon.CustomFieldId
                    WHERE   [File].Id = SelectionCache.RecordId
                    AND SelectionCache.UUID = @UUID
                    AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
                    AND     [File].Id <> 0 
                    AND     [File].FileTypeId = FileType2Lexicon.FileTypeId 
                    AND     FileType2Lexicon.LexiconId = @LexiconId
                    AND     [File].ClientIdString = [People].ClientIdString
                    AND     CustomFieldValue.Value <> ''),

            SolicitorCTE AS(
                SELECT [People].Name AS SolicitorName, [People].InvertedName as InvertedSolicitorName, [File].Id
                FROM SelectionCache, [File]
                INNER JOIN [People2File]
                ON [People2File].FileId = [File].Id
                INNER JOIN [Role2Lexicon]
                ON [Role2Lexicon].RoleId = [People2File].RoleId
                INNER JOIN [People]
                ON [People].Id = [People2File].PeopleId
                WHERE 
                [File].Id = SelectionCache.RecordId
                AND SelectionCache.UUID = @UUID
                AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
                AND [File].Id <> 0
                AND [Role2Lexicon].Label = 'Solicitor'),        

            ArrestingOfficerCTE AS(
                SELECT COALESCE(ROW_NUMBER() OVER(PARTITION BY [File].Id ORDER BY [People].InvertedName ASC), NULL) AS RowNumber, COALESCE([People].Name, NULL) AS ArrestingOfficerName, COALESCE([People].CompanyName, NULL)  AS ArrestingOfficerCompany, [File].Id
                FROM SelectionCache, [File]
                INNER JOIN [People2File]
                ON [People2File].FileId = [File].Id
                INNER JOIN [Role2Lexicon]
                ON [Role2Lexicon].RoleId = [People2File].RoleId
                INNER JOIN [People]
                ON [People].Id = [People2File].PeopleId
                WHERE 
                [File].Id = SelectionCache.RecordId
                AND SelectionCache.UUID = @UUID
                AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
                AND [File].Id <> 0
                AND [Role2Lexicon].Label = 'Arresting Officer'),

                BillCTE As(
                    SELECT [File].Id, SUM(Bill.BilledFee) as BilledFeeTotal, SUM(Bill.BilledExpense) as BilledExpenseTotal, SUM(Bill.BilledTax1) as BilledTax1Total, SUM(Bill.BilledTax2) as BilledTax2Total, SUM(Bill.BilledInterest) as BilledInterestTotal
                        FROM SelectionCache, [File]
                        INNER JOIN [Bill]
                        ON [File].Id = Bill.FileId
                        WHERE
                        [File].Id = SelectionCache.RecordId
                        AND SelectionCache.UUID = @UUID
                        AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
                        AND [File].Id <> 0
                        GROUP BY [File].Id),

                BillPaymentCTE As(
                    SELECT [File].Id, SUM(BillPayment.FeeAmount) as PaidFeeAmountTotal, SUM(BillPayment.ExpenseAmount) as PaidExpenseAmountTotal, SUM(BillPayment.Tax1) as PaidTax1Total, SUM(BillPayment.Tax2) as PaidTax2Total, SUM(BillPayment.InterestAmount) as PaidInterestAmountTotal
                        FROM SelectionCache, [File]
                        INNER JOIN [Bill]
                        ON [File].Id = Bill.FileId
                        INNER JOIN [BillPayment]
                        ON [Bill].Id = BillPayment.BillId
                        WHERE
                        [File].Id = SelectionCache.RecordId
                        AND SelectionCache.UUID = @UUID
                        AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
                        AND [File].Id <> 0
                        GROUP BY [File].Id),

                BillAdjustmentCTE AS(
                     SELECT [File].Id, COALESCE((SUM(BillAdjustment.FeeAmount)), 0) as AdjustFeeAmountTotal, SUM(COALESCE(BillAdjustment.ExpenseAmount, 0)) as AdjustExpenseAmountTotal
                        FROM SelectionCache, [File]
                        INNER JOIN [Bill]
                        ON [File].Id = Bill.FileId
                        INNER JOIN [BillAdjustment]
                        ON [Bill].Id = BillAdjustment.BillId
                        WHERE
                        [File].Id = SelectionCache.RecordId
                        AND SelectionCache.UUID = @UUID
                        AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
                        AND [File].Id <> 0
                        GROUP BY [File].Id),

            PivotCTE AS(
                SELECT *
                FROM
                (Select Id, FieldLabel, FieldValue FROM DOACTE) AS Source
                PIVOT(
                MAX(FieldValue) FOR FieldLabel IN ([Date_Arrest], [Graphic_Client], [Ticket_1], [Ticket_2], [Ticket_3], [Ticket_4], [Ticket_5], [Charge_1], [Charge_2], [Charge_3], [Charge_4], [Charge_5])) as Pvt
                )

            SELECT DOACTE.*, 
                    COALESCE(ArrestingOfficerCTE.ArrestingOfficerCompany, NULL)AS ArrestingOfficerCompany, COALESCE(ArrestingOfficerCTE.ArrestingOfficerName, NULL) AS ArrestingOfficerName, 
                    COALESCE(SolicitorCTE.SolicitorName, NULL) as SolicitorName, COALESCE(SolicitorCTE.InvertedSolicitorName, NULL) as InvertedSolicitorName, 
                    PivotCTE.[Date_Arrest], dbo.GetImagebyId(PivotCTE.[Graphic_Client]) as Photo, PivotCTE.[Ticket_1], PivotCTE.[Ticket_2], PivotCTE.[Ticket_3], PivotCTE.[Ticket_4], PivotCTE.[Ticket_5], PivotCTE.[Charge_1], PivotCTE.[Charge_2], PivotCTE.[Charge_3], PivotCTE.[Charge_4], PivotCTE.[Charge_5], 
                    BillCTE.BilledFeeTotal, BillCTE.BilledExpenseTotal, BillCTE.BilledTax1Total, BillCTE.BilledTax2Total, BillCTE.BilledInterestTotal, (BillCTE.BilledFeeTotal + BillCTE.BilledExpenseTotal + BillCTE.BilledTax1Total + BillCTE.BilledTax2Total + BillCTE.BilledInterestTotal) AS BillTotal,
                    BillPaymentCTE.PaidFeeAmountTotal, BillPaymentCTE.PaidExpenseAmountTotal, BillPaymentCTE.PaidTax1Total, BillPaymentCTE.PaidTax2Total, BillPaymentCTE.PaidInterestAmountTotal, (BillPaymentCTE.PaidFeeAmountTotal + BillPaymentCTE.PaidExpenseAmountTotal + BillPaymentCTE.PaidTax1Total + BillPaymentCTE.PaidTax2Total + BillPaymentCTE.PaidInterestAmountTotal) AS BillPaymentTotal,
                    BillAdjustmentCTE.AdjustFeeAmountTotal, BillAdjustmentCTE.AdjustExpenseAmountTotal, (BillAdjustmentCTE.AdjustFeeAmountTotal + BillAdjustmentCTE.AdjustExpenseAmountTotal) AS BillAdjustmentTotal,
                    ((BillCTE.BilledFeeTotal + BillCTE.BilledExpenseTotal + BillCTE.BilledTax1Total + BillCTE.BilledTax2Total + BillCTE.BilledInterestTotal) + (BillAdjustmentCTE.AdjustFeeAmountTotal + BillAdjustmentCTE.AdjustExpenseAmountTotal) - (BillPaymentCTE.PaidFeeAmountTotal + BillPaymentCTE.PaidExpenseAmountTotal + BillPaymentCTE.PaidTax1Total + BillPaymentCTE.PaidTax2Total + BillPaymentCTE.PaidInterestAmountTotal)) AS AccountsReceivable
                FROM DOACTE
                INNER JOIN 
                PivotCTE
                ON DOACTE.Id = PivotCTE.Id
                LEFT JOIN
                SolicitorCTE
                ON DOACTE.Id = SolicitorCTE.Id
                LEFT JOIN
                ArrestingOfficerCTE
                ON DOACTE.Id = ArrestingOfficerCTE.Id
                LEFT JOIN
                BillCTE
                ON DOACTE.Id = BillCTE.Id 
                LEFT JOIN
                BillAdjustmentCTE
                ON DOACTE.Id = BillAdjustmentCTE.Id
                LEFT JOIN
                BillPaymentCTE
                ON DOACTE.Id = BillPaymentCTE.Id
                WHERE DOACTE.RowNumber = 1
                AND (ArrestingOfficerCTE.RowNumber < 2
                OR ArrestingOfficerName IS NULL)




        ELSE

        DELETE SelectionCache 
            WHERE UUID = @UUID
            AND   TableCode = @TableCode
    END

 RETURN
4

1 回答 1

1

这是因为您在 CTE 级别进行 COALESCE(因此 CTE 将为 NULL 值返回 0),但随后您正在对 CTE 执行 LEFT JOIN。如果 CTE 没有返回任何行,则 LEFT JOIN 将返回 NULL。您需要在 SELECT 语句级别 COALESCE 以弥补这一点。

于 2013-06-18T15:45:59.633 回答