Столбец Total в запросе PIVOT игнорирует запись со значениями NULL - PullRequest
0 голосов
/ 17 апреля 2019

Столбец Итого в этом перекрестном запросе суммирует двенадцать столбцов M1, M2 .... M12 слева от него.Проблема состоит в том, что столбец Total возвращает значение 0 для всех записей, где хотя бы один из столбцов M является нулевым значением.Я добавил условие isnull везде, где я думаю, что это необходимо, но оно все еще не работает.

    SELECT 
         [Prop] as 'Company'
        ,[Category_EXCO] as 'Account Category'
        ,[CenterNum] as 'CenterNumber'
        ,[AccountNum] as 'Account'
        ,[Account_Description] as 'Description'
        ,case when [Is_Revenue] = 1 then 'TRUE' else 'FALSE' end as 'RevorExp'
        , case when [Is_Above_EBITDA] = 1 then 'TRUE' else 'FALSE' end as 'EBITDA'
        ,format(isnull(M1,0),'#,#0.00') as M1
        ,format(isnull(M2,0),'#,#0.00') as M2
        ,format(isnull(M3,0),'#,#0.00') as M3
        ,format(isnull(M4,0),'#,#0.00') as M4
        ,format(isnull(M5,0),'#,#0.00') as M5
        ,format(isnull(M6,0),'#,#0.00') as M6
        ,format(isnull(M7,0),'#,#0.00') as M7
        ,format(isnull(M8,0),'#,#0.00') as M8
        ,format(isnull(M9,0),'#,#0.00') as M9
        ,format(isnull(M10,0),'#,#0.00') as M10
        ,format(isnull(M11,0),'#,#0.00') as M11
        ,format(isnull(M12,0),'#,#0.00') as M12 
        ,format(isnull(M1+M2+M3+M4+M5+M6+M7+M8+M9+M10+M11+M12,0),'#,#0.00') as TOTAL
        ,[Division_Description] as Division
        ,case when [Is_F&B1] = 1 then 'F&B Consolidated' else 'Non-F&B' end as Division_1
        ,[Group_Description] as 'Group'
        ,case when [Group_Description] = 'RWNY Operations' THEN 'NEW YORK' ELSE 'FLORIDA' END as 'Region'
        ,CONCAT ([AccountNum],'_',[CenterNum]) as 'Acct+Center'

--The code generates a flat table to be used to fill the columns in the crosstab query
    FROM 
        (
        SELECT 
         tblCenters.prop
        ,tblAccounts.Category_EXCO
        ,tblCenters.CenterNum
        ,tblAccounts.AccountNum
        ,tblAccounts.Account_Description
        ,tblAccounts.Is_Revenue
        ,tblAccounts.Is_Above_EBITDA
        ,tblCenters.Division_Description
        ,tblCenters.[Is_F&B1]
        ,tblCenters.Group_Description
        ,isnull(tblsap.Entry,0) Entry
        ,MX
            FROM GA_Financial.dbo.tblSAP 

            left join DateTable on tblSAP.MDY = DateTable.MDY
            left join tblAccounts on tblSAP.AccountNum = tblAccounts.AccountNum
            left join tblCenters on tblSAP.CenterNum = tblCenters.CenterNum and tblSAP.Prop_SAP = tblCenters.PROP_SAP

            WHERE tblAccounts.Is_Above_EBITDA = 1
            AND tblSAP.BudAct = 'A'

        ) 

    AS Tab1
--The code below breaks down column "Entry" into twelve individual monthly columns and fills columns M1 through M12
    PIVOT
        (

        SUM(entry) for MX in (M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12)
        ) as TAb2

        where Prop = 'RWNY'
...