案例陈述中的多个分组&排序

时间:2012-09-10 16:06:08

标签: tsql grouping rollup

我正在进行聚合存储过程,我的最终选择如下:

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

然而,这似乎不起作用。我想订购它,以便它在每个portfolioID子总数之间下降ABS值。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

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

我怀疑排序问题与显示的列实际上是TotalCashPctNAV的SUM的事实有关,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