5

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

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 标签和两个总和是该特定行上唯一的数据位,这可能吗?

4

2 回答 2

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 
于 2012-09-12T19:03:27.647 回答
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 表达式来确定哪些行是总计。例如:

CASE WHEN GROUPING(Custodian) + GROUPING(PortfolioId) > 0 THEN '' 
     ELSE PortfolioID END AS PortfolioID
于 2012-09-12T19:04:01.993 回答