我目前正在处理聚合存储过程,我的最终选择如下所示:
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 值。
任何帮助,将不胜感激。