从sql汇总中的小计/总计中删除空值

时间:2012-09-12 14:12:23

标签: tsql null rollup

我目前有一个脚本,下面使用汇总聚合一些数据:

SELECT 
        CASE 
            WHEN GROUPING(Custodian) = 1 
                THEN 'Grand Total'
            WHEN GROUPING(PortfolioID) = 1
                THEN Custodian+''+'Total'
            ELSE Custodian

        END AS Custodian

    ,   PortfolioID
    ,   PortfolioBaseCCY
    ,   [Date]
    ,   SUM(AmountTotalBaseEquiv) AS AmountTotalBaseEquiv
    ,   ExchangeRate
    ,   AmountTotalBaseEquivUSD
    ,   PortfolioNAVUSD
    ,   SUM(TotalCashPctNAV) AS TotalCashPctNAV 

FROM @ResultSet
WHERE TotalCashPctNAV > 5
GROUP BY Custodian
    ,   PortfolioID
    ,   PortfolioBaseCCY
    ,   [Date]
    ,   AmountTotalBaseEquiv
    ,   ExchangeRate
    ,   AmountTotalBaseEquivUSD
    ,   PortfolioNAVUSD 
    ,   TotalCashPctNAV WITH ROLLUP

HAVING GROUPING_ID(Custodian
    ,   PortfolioID
    ,   PortfolioBaseCCY
    ,   [Date]
    ,   AmountTotalBaseEquiv
    ,   ExchangeRate
    ,   AmountTotalBaseEquivUSD
    ,   PortfolioNAVUSD 
    ,   TotalCashPctNAV) IN (1,255,511)

ORDER BY CASE WHEN GROUPING(Custodian) = 1 THEN 2 ELSE 1 END, Custodian, TotalCashPctNAV DESC, PortfolioID

这会返回数据,例如:

Custodian   PortfolioID PortfolioBaseCCY Date         AmountTotalBaseEquiv  ExchangeRate    AmountTotalBaseEquivUSD PortfolioNAVUSD TotalCashPctNAV
XXXX        TEST        USD              11/09/2012   85708860.21           1               85708860.21             370253861.3     23.15
XXXX  Total NULL        NULL             NULL         85708860.21           NULL            NULL                    NULL            23.15
ZZZZ        TEST1       GBP              11/09/2012   48427.91              0.6225          77795.84                77795.84        100
ZZZZ        TEST2       GBP              11/09/2012   7772.61               0.6225          12486.12                12486.12        100
ZZZZ        TEST3       USD              11/09/2012   1832627.81            1               1832627.81              17343500.68     10.56
ZZZZ  Total NULL        NULL             NULL         1888828.33            NULL            NULL                    NULL            210.56
Grand Total NULL        NULL             NULL         310273031.4           NULL            NULL                    NULL            1051.71

我想要的是NULLS成为''以便只有Total标签和两个总计是该特定行上的唯一数据位,这可能吗?

2 个答案:

答案 0 :(得分:4)

您可以在SQL中使用ISNull()函数将NULL替换为空格,如下所示:

SELECT 
    CASE 
        WHEN GROUPING(Custodian) = 1 
            THEN 'Grand Total'
        WHEN GROUPING(PortfolioID) = 1
            THEN Custodian+''+'Total'
        ELSE Custodian

    END AS Custodian

,   isNUll(PortfolioID,'')
,   isNull(PortfolioBaseCCY,'')
,   isNull([Date],'')
,   SUM(AmountTotalBaseEquiv) AS AmountTotalBaseEquiv
,   isNull(ExchangeRate,'')
,   isNull(AmountTotalBaseEquivUSD,'')
,   isNull(PortfolioNAVUSD,'')
,   SUM(TotalCashPctNAV) AS TotalCashPctNAV 

答案 1 :(得分:1)

如果您可以将NULL值显示为数据行和总行中的空字符串,则可以将所有非字符串列转换为字符串并使用COALESCE(或ISNULL),如:

SELECT  
        CASE  
            WHEN GROUPING(Custodian) = 1  
                THEN 'Grand Total' 
            WHEN GROUPING(PortfolioID) = 1 
                THEN Custodian+' '+'Total' 
            ELSE Custodian 

        END AS Custodian 

    ,   COALESCE(PortfolioID,'') AS PortfolioID
    ,   COALESCE(PortfolioBaseCCY,'') AS  PortfolioBaseCCY
    ,   COALESCE(CONVERT(char(10),[Date],101),'') AS [Date]
    ,   SUM(AmountTotalBaseEquiv) AS AmountTotalBaseEquiv 
    ,   COALESCE(CONVERT(char(10),ExchangeRate),'') AS ExchangeRate
    ,   COALESCE(CONVERT(char(20),AmountTotalBaseEquivUSD),'') AS AmountTotalBaseEquivUSD
    ,   COALESCE(CONVERT(char(20),PortfolioNAVUSD),'') AS PortfolioNAVUSD
    ,   SUM(TotalCashPctNAV) AS TotalCashPctNAV

FROM @ResultSet 
WHERE TotalCashPctNAV > 5 
GROUP BY Custodian 
    ,   PortfolioID 
    ,   PortfolioBaseCCY 
    ,   [Date] 
    ,   AmountTotalBaseEquiv 
    ,   ExchangeRate 
    ,   AmountTotalBaseEquivUSD 
    ,   PortfolioNAVUSD  
    ,   TotalCashPctNAV WITH ROLLUP 

HAVING GROUPING_ID(Custodian 
    ,   PortfolioID 
    ,   PortfolioBaseCCY 
    ,   [Date] 
    ,   AmountTotalBaseEquiv 
    ,   ExchangeRate 
    ,   AmountTotalBaseEquivUSD 
    ,   PortfolioNAVUSD  
    ,   TotalCashPctNAV) IN (1,255,511) 

ORDER BY CASE WHEN GROUPING(Custodian) = 1 THEN 2 ELSE 1 END, Custodian, TotalCashPctNAV 

否则,您可以像使用Custodian一样使用案例表达式来确定哪些行是总计。例如:

CASE WHEN GROUPING(Custodian) + GROUPING(PortfolioId) > 0 THEN '' 
     ELSE PortfolioID END AS PortfolioID