0

我目前正在处理聚合存储过程,我的最终选择如下所示:

SELECT 
        CASE 
            WHEN GROUPING(Custodian) = 1 
                THEN 'Grand 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 ABS(TotalCashPctNAV) DESC

但是我想在 CASE 语句中添加另一个分组,即:

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

但是它不起作用,因为第二种情况不返回值,这是为什么呢?

我还想通过以下方式在上述选择中订购 TotalCashPctNAV:

ORDER BY ABS(TotalCashPctNAV) DESC

但是,这似乎不起作用。我希望它排序,以便它在每个投资组合 ID 子总计之间降序排列 ABS 值。

任何帮助,将不胜感激。

4

1 回答 1

1

将第二个分组添加到 CASE 语句中按我的预期工作。AmounttotalBaseEquiv 和 TotalCashPct 列填充了与 Grand Total 行一样的值,并且 Custodian 列的命名似乎适合各个 Custodian 总计。

我怀疑排序问题与显示的列实际上是 TotalCashPctNAV 的总和有关,它已被别名回“TotalCashPctNAV”。ORDER BY 语句针对的是实际的 TotalCashPctNAV 列而不是别名 SUM。

有趣的是,如果 ORDER BY 语句不使用 ABS,那么排序就是我所期望的。我不确定为什么 ABS 会导致它的顺序不同,因为我在我的例子中使用了所有积极的术语。

例子:

DECLARE @ResultSet TABLE
(
    Custodian VARCHAR(10),
    PortfolioID INT,
    PortfolioBaseCCY INT,
    [Date] DATETIME,
    AmountTotalBaseEquiv NUMERIC(8,2),
    PortfolioNAVUSD NUMERIC(8,2),
    TotalCashPctNAV NUMERIC(8,2),
    ExchangeRate NUMERIC(8,2),
    AmountTotalBaseEquivUSD NUMERIC(8,2)
)

INSERT INTO @ResultSet SELECT 'Anne', 1, 1, '6/1/2012', '600.00', '643.45', '3.78', '2.00', '353.00'
INSERT INTO @ResultSet SELECT 'Bob', 2, 1, '6/13/2012', '745.00', '9.42', '36.70', '1.70', '353.00'
INSERT INTO @ResultSet SELECT 'Carl', 3, 1, '6/27/2012', '488.00', '9875.99', '6.60', '1.80', '353.00'
INSERT INTO @ResultSet SELECT 'Doug', 4, 1, '6/29/2012', '87.00', '45.98', '69.12', '2.10', '353.00'
INSERT INTO @ResultSet SELECT 'Elmer', 5, 1, '6/30/2012', '775.00', '78.47', '69.78', '2.41', '353.00'
INSERT INTO @ResultSet SELECT 'Bob', 6, 1, '6/30/2012', '775.00', '78.47', '69.78', '2.41', '353.00'

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 TotalCashPctNAVSUM --I appended SUM to the alias

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 ABS(TotalCashPctNAV) DESC  --works with TotalCashPctNAV but not TotalCashPctNAVSUM
--ORDER BY TotalCashPctNAV DESC  --works as initially expected
于 2012-09-11T04:38:51.277 回答